1 if ($ ("Input[name=item][value= ' Val ')"). attr (' checked ') ==true)//Determine if the name is the control Name property, value is the control Value property can not specify the property value, Because the value of a set of checkboxes will hold its corresponding ID in the database, it is best to write the IF ($ ("Input[name=row_checkbox]") as follows. attr (' checked ') ==true) {alert ("J");} Else{alert ("Please select data! ");} Alternatively, you can write if ($ ("[Name=row_checkbox]"). attr (' checked ') ==true) radio using:
<
body
>
<
form
id
=
"form1"
>
<
input
type
=
"radio"
name
=
"sex"
value
=
"男"
/>男
<
input
type
=
"radio"
name
=
"sex"
value
=
"女"
/>女
<
br
/>
<
input
type
=
"radio"
name
=
"list"
value
=
"十分满意"
/>十分满意
<
input
type
=
"radio"
name
=
"list"
value
=
"满意"
/>满意
<
input
type
=
"radio"
name
=
"list"
value
=
"不满意"
/>不满意
<
input
type
=
"radio"
name
=
"list"
value
=
"非常差"
/>非常差
<
br
/>
<
input
type
=
"submit"
value
=
"submit"
id
=
"btnSubmit"
/>
</
form
>
</
body
>
</
html
>
$(function(){ $("#btnSubmit"). Click (function(){ varval=$ (' input:radio[name= ' "sex"): Checked‘). Val (); if(val==NULL) {alert ("Nothing is chosen!"); return false; } Else{alert (val); } varList= $ (' input:radio[name= ' list "]:checked '). Val (); if(list==NULL) {alert ("Please select a!"); return false; } Else{alert (list); } }); });
$ (' #checkbox '). attr (' checked '); The return is checked or undefined, not the original true and False, and the workaround for this problem is as follows
In the previous version of JQ1.6, we would write our code like this:
- <input type=' checkbox ' id=' cb '/>
- <script>
- Gets whether to select
- var isChecked = $ (' #cb '). attr (' checked ');
- Set the selected
- $ (' #cb '). attr ('checked ',true);
- </script>
This is not a problem before JQ1.6, but when we upgrade JQ1.6 to a higher version, the problem comes, and we find that:
$ (' #cb'). attr (' checked '); The return is checked or undefined, not the original true and false. And the checked property is initialized when the page is initialized, and does not change as the state changes . So if the checkbox is initially selected, the return is checked, and if it is not selected at first, then the return is undefined
- <input type=' checkbox ' id=' cb '/>
- <script>
- Gets whether to select
- var isChecked = $ (' #cb '). Prop (' checked ');
- Or
- var isChecked = $ (' #cb '). Is (": Checked");
- Set the selected
- $ (' #cb '). Prop ('checked ',true);
- </script>
Analysis of the reasons, it can be understood:
It distinguishes between "attribute" and "property", which refers to "name,id" and so on, which refers to "SelectedIndex, TagName, NodeName" and so on.
After JQ1.6, the attr method can be used to obtain the properties, and the prop method to obtain the characteristics
- $ ("#cb"). attr ("tagName"); //undefined
- $ ("#cb"). Prop ("tagName"); //input
MORE: http://blog.sina.com.cn/s/blog_70491fc60100t5kw.html