I am confused about this code its s开发者_如何转开发elf explanatory what its supposed to do but it doesn't do it
<input name="property" type="radio" checked value="1" />Station Masters House
<input name="property" type="radio" value="2" />Railway Carriage
<br><br>
<div id="fillin">press</div>
$("#fillin").click(function() {
if ($('input[name=property]').val() == "1")
{
alert($('input[name=property]').val());
} else if ($('input[name=property]').val() == "2") {
alert($('input[name=property]').val());
}
});
example here
http://jsfiddle.net/BFf5H/40/
input[name=property].length === 2
... therefore, .val()
won't work. (.val
pulls the value of the first element in the DOM that it finds that matches the requested selector, except in the case of a <select multiple>
element.)
Try input[name=property]:checked
instead.
$("#fillin").click(function() {
if ($('input[name=property]:checked').val() == "1")
{
alert($('input[name=property]:checked').val());
} else if ($('input[name=property]:checked').val() == "2") {
alert($('input[name=property]:checked').val());
}
});
Better yet, cache the things you need, so you are only hitting the DOM (or in this case, jQuery's cache) once:
$("#fillin").click(function() {
var val = $('input[name=property]:checked').val();
if ( val == "1") {
alert(val);
} else if (val == "2") {
alert(val);
}
});
you should do
if ($('input[name=property]:checked').val() == "1")
精彩评论