Home > Web Front-end > JS Tutorial > body text

jQuery operation input type=radio implementation code

高洛峰
Release: 2017-02-11 17:11:07
Original
1735 people have browsed it

as follows:

<input type="radio" name="city" value="BeiJing">北京 
<input type="radio" name="city" value="TianJin">天津 
<input type="radio" name="city" value="NanJing">南京 
<input type="radio" name="city" value="YangZhou">扬州 
<input type="radio" name="city" value="SuZhou">苏州
Copy after login

1. Get the value of the selected radio:

$("input[name=&#39;city&#39;]:checked").val();
Copy after login

Use the element selector and then use the attribute filter name= 'city', and finally use:checked to select the selected elements.

2. Set the selected state for the radio with the specified value:

$("input[name=&#39;city&#39;][value=&#39;YangZhou&#39;]").attr("checked",true);
Copy after login

Set the selected state for the radio with name="city" and value="YangZhou".

3. Cancel the selection status of the radio with name="city":

$(&#39;input[name="city"]:checked&#39;).attr("checked",false);
Copy after login

4. Get the number of radios with name="city":

$("input[name=&#39;city&#39;]").length;
Copy after login

5. Get the radio with name="city" and the index is even:

$("input[name=&#39;city&#39;]:even");
Copy after login

The index starts from 0.

6. Get the radio with name="city" and the index is odd:

$("input[name=&#39;city&#39;]:odd");
Copy after login

The index starts from 0.

7. Iterate radio:

$("input[name=&#39;city&#39;]").each(function(i,obj){ 
//i,迭代的下标,从0开始 
//obj,当前的对象(HTMLInputElement),可以使用obj.value格式获取属性值 
//$(this);当前jQuery对象,可以使用$(this).val()获取属性值 
});
Copy after login

Iterate the radio with name="city".

8. Disable radio:

$("input[name=&#39;city&#39;]").attr("disabled",true);
Copy after login

Disable the radio with name="city".

9. Enable radio:

$("input[name=&#39;city&#39;]").attr("disabled",false);
Copy after login

Enable the radio with name="city".

For more jQuery operation input type=radio implementation code related articles, please pay attention to 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