About jQuery.Attr () and.Prop (problem)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A few words to write code in IE8 to normal operation, chrome and FF are not, friends say this is RP ah, depressed!

In fact, the function demand is such, two radio: male and female, a button: reset. Start page default selected male, after the user selection and click the reset button, need to revert to the default state.

<input type="radio" id="hRdMale" checked="checked" name="sex" value="male" /><label for="hRdMale">Male</label>
<input type="radio" id="hRdFemale" name="sex" value="female" /><label for="hRdFemale">Female</label>
<button id="reSet">Reset</button>

My JS code like this:

$("#reSet").click(function() {
  $("input[name='sex']:first").attr("checked",true);
});

The result is what I mentioned in the beginning, I asked several people said that was not clear, then check the API know add.Prop in version of the jQuery1.6+ () method, and the.Attr (seemingly) no difference, can be used to get and set the attribute value. Later learned that.Prop () method for attribute to the Boolean value, the official explanation selectedIndex,   tagName,   nodeName,   nodeType,   ownerDocument,   defaultChecked, and defaultSelected  with the.Prop () method attribute. In addition, see online there are a lot of say “ add the attribute name of the property will be effective should use.Prop (”), I didn't verify other properties, it is the "checked" attribute is added in effect. So, the red points should be a.Attr () and.Prop (difference).

So, the above the click event method code into the OK.

$("input[name='sex']:first").prop("checked",true);

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Quinn at November 29, 2013 - 12:53 PM