Jquery gets form values such as Input,select and other methods

Source: Internet
Author: User
Tags tagname

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:

    1. <input type=' checkbox ' id=' cb '/>
    2. <script>
    3. Gets whether to select
    4. var isChecked = $ (' #cb '). attr (' checked ');
    5. Set the selected
    6. $ (' #cb '). attr ('checked ',true);
    7. </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

  1. <input type=' checkbox ' id=' cb '/>
  2. <script>
  3. Gets whether to select
  4. var isChecked = $ (' #cb '). Prop (' checked ');
  5. Or
  6. var isChecked = $ (' #cb '). Is (": Checked");
  7. Set the selected
  8. $ (' #cb '). Prop ('checked ',true);
  9. </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

      1. $ ("#cb"). attr ("tagName"); //undefined
      2. $ ("#cb"). Prop ("tagName"); //input

MORE: http://blog.sina.com.cn/s/blog_70491fc60100t5kw.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.