JQuery:在单选按钮选择上显示 div
当用户选择单选按钮组中的特定单选按钮(Other
)时,我尝试使用 jQuery 显示 div。
html 如下
<div id="countries">
<input id="Washington_D.C" type="radio" name="location" value="Washington">Washington D.C</input>
<input id="Other" type="radio" name="location" value="">Other</input>
<div id="other locations" style="display: none">
</div>
</div>
使用 JQuery 代码:
$(document).ready(function(){
$("radio[@name='location']").change(function(){
if ($("radio[@name='location']:checked").val() == 'Other')
$("#county_drop_down").show();
});
});
但是当我选择单选按钮 Other
时,它没有显示 div 其他位置。
I am trying to use jQuery to show a div when the user selects a particular radio button (Other
) within a radiobutton group.
The html is below
<div id="countries">
<input id="Washington_D.C" type="radio" name="location" value="Washington">Washington D.C</input>
<input id="Other" type="radio" name="location" value="">Other</input>
<div id="other locations" style="display: none">
</div>
</div>
Using the JQuery code:
$(document).ready(function(){
$("radio[@name='location']").change(function(){
if ($("radio[@name='location']:checked").val() == 'Other')
$("#county_drop_down").show();
});
});
But it's not showing the div other locations when I select the radiobutton Other
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要为其指定值
Other
。因此,不是这样,
而是这样
乍一看,您的 jQuery 代码看起来不错。然而,我更喜欢
click
而不是change
,因为如果没有预选的方法,change
不会在第一次点击时被触发。You need to give it a value of
Other
.Thus, not so
but so
At first glance, your jQuery code looks fine. I would however prefer
click
abovechange
, sincechange
doesn't get fired on 1st click if there's no means of preselection.尝试将
单选按钮值设置为“其他”以使此代码正常工作。
Try
set the radio button value to Other for this code to work.