Home > Web Front-end > JS Tutorial > Detailed example of how Jquery obtains the radio selected value

Detailed example of how Jquery obtains the radio selected value

黄舟
Release: 2017-07-27 14:20:12
Original
2148 people have browsed it


#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.

Radio


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

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


2. Set the first Radio as the selected value:

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

or

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

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

3. Set the last one Radio is the selected value:

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

or

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

4. Set any radio as the selected value according to the index value:

$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....
Copy after login

or

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

5. Set Radio to the selected value according to the Value value

$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');
Copy after login

or

$("input[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');
Copy after login

6. Delete the Radio whose Value is rd2

$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").remove();
Copy after login

7. Delete the Radio

$("input:radio").eq(索引值).remove();索引值=0,1,2....
Copy after login

If you delete the third Radio:$("input:radio").eq(2).remove();

8. Traverse Radio

$('input:radio').each(function(index,domEle){
     //写入代码
});
Copy after login

DropDownList



##

1. 获取选中项:

获取选中项的Value值:

 $('select#sel option:selected').val();
Copy after login

或者

$('select#sel').find('option:selected').val();
Copy after login

获取选中项的Text值:

   $('select#seloption:selected').text();
Copy after login

或者

 $('select#sel').find('option:selected').text();
Copy after login

2. 获取当前选中项的索引值:

$('select#sel').get(0).selectedIndex;
Copy after login

3. 获取当前option的最大索引值:

$('select#sel option:last').attr("index")
Copy after login

4. 获取DropdownList的长度:

$('select#sel')[0].options.length;
Copy after login

或者

$('select#sel').get(0).options.length;
Copy after login

5. 设置第一个option为选中值:

$('select#sel option:first').attr('selected','true')
Copy after login

或者

 $('select#sel')[0].selectedIndex = 0;
Copy after login

6.   设置最后一个option为选中值:

 


The above is the detailed content of Detailed example of how Jquery obtains the radio selected value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template