Home  >  Article  >  Web Front-end  >  JQuery radio (radio button) operation method summary_jquery

JQuery radio (radio button) operation method summary_jquery

WBOY
WBOYOriginal
2016-05-16 16:03:511369browse

As Jquery becomes more and more useful, more and more friends use it. In the Web, due to the high frequency of use of controls such as CheckBox, Radiobutton, and DropDownList, it is related to the operation of these controls in Jquery. Since the version of Jquery is updated very quickly, the way of writing the code has also changed a lot. The following Jquery code is suitable for query1.4 and above.

1. Get the selected value, three methods are available:

Copy code The code is as follows:

$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();

2. Set the first Radio as the selected value:
Copy code The code is as follows:

$('input:radio:first').attr('checked', 'checked');

or
Copy code The code is as follows:

$('input:radio:first').attr('checked', 'true');

Note: attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)

3. Set the last Radio as the selected value:

Copy code The code is as follows:

$('input:radio:last').attr('checked', 'checked');

or
Copy code The code is as follows:

$('input:radio:last').attr('checked', 'true');

4. Set any radio as the selected value according to the index value:
Copy code The code is as follows:

$('input:radio').eq(index value).attr('checked', 'true');Index value=0,1,2....

or
Copy code The code is as follows:

$('input:radio').slice(1,2).attr('checked', 'true');

5. Set Radio to the selected value according to the Value value
Copy code The code is as follows:

$("input:radio[value='rd2']").attr('checked','true');

or
Copy code The code is as follows:

$("input[value='rd2']").attr('checked','true');

6. Delete the Radio whose Value is rd2
Copy code The code is as follows:

$("input:radio[value='rd2']").remove();

7. Which Radio to delete
Copy code The code is as follows:

$("input:radio").eq(index value).remove(); index value=0,1,2....

For example, delete the third Radio:$("input:radio").eq(2).remove();

8. Traverse Radio

Copy code The code is as follows:

$('input:radio').each(function(index,domEle){

//Write code

});

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn