[英]jquery radio buttons not switching?
I am confused about this code its self 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 这里的例子
input[name=property].length === 2
... therefore, .val()
won't work. input[name=property].length === 2
...因此, .val()
将不起作用。 ( .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.) (
.val
在DOM中找到的第一个元素的值与所请求的选择器匹配,除非<select multiple>
元素。)
Try input[name=property]:checked
instead. 尝试
input[name=property]:checked
。
$("#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: 更好的是,缓存所需的内容,因此您只需访问一次DOM(在本例中为jQuery的缓存):
$("#fillin").click(function() {
var val = $('input[name=property]:checked').val();
if ( val == "1") {
alert(val);
} else if (val == "2") {
alert(val);
}
});
你应该做
if ($('input[name=property]:checked').val() == "1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.