Javascript jquery单选按钮不切换?
我对这段代码感到困惑——它可以解释它应该做什么,但它没有做到Javascript jquery单选按钮不切换?,javascript,jquery,Javascript,Jquery,我对这段代码感到困惑——它可以解释它应该做什么,但它没有做到 <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>
<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());
}
});
站长之家酒店
铁路车厢
按
$(“#fillin”)。单击(函数(){
if($('input[name=property]')。val()=“1”)
{
警报($('input[name=property]').val();
}else if($('input[name=property]')。val()=“2”){
警报($('input[name=property]').val();
}
});
这里的例子
输入[name=property]。长度===2
。。。因此,这是行不通的。(.val
提取它在DOM中找到的与请求的选择器匹配的第一个元素的值,除非是
元素。)
请尝试输入[name=property]:改为选中
$("#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());
}
});
更好的是,缓存您需要的东西,这样您只需访问DOM(或者在本例中是jQuery的缓存)一次:
你应该这样做
if ($('input[name=property]:checked').val() == "1")
if ($('input[name=property]:checked').val() == "1")