jquery 隐藏仅有效一次
我有一个最初隐藏的区域,应该根据字段的内容出现/消失。
因此,如果用户选择通过信用卡付款,那么额外的字段就会打开,如果没有,那么它们就会消失。
问题是它只能显示它们而不是隐藏它们。
有人可以看这里:http://bit.ly/dOCNQQ
提前致谢!
$(function(){
$('#CCinfo').hide();
$('#paymethod').change(function(){
var payme = $(this).val();
if (payme=='CreditCard') {
$('#CCinfo').show("slow");
} else {
$('#CCinfo').hide();
}
});
});
<tr>
<td colspan="2" align="right">
<strong>pay by:</strong><br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="CreditCard"/>credit card<br />
<tr>
<td colspan="2" align="right"><strong>pay by:</strong><br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="CreditCard"/>credit card<br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="Check"/>cash<br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="At Dinner"/>check
</td>
</tr>
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="Check"/>cash<br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="At Dinner"/> check
</td>
</tr>
i have an area which is initially hidden, and should appear/disappear based on a field's content.
so if the user selects pay by credit card - then the extra fields open, if not - then they disappear.
the problem is that it only works to show them - not hide.
can someone pls see here: http://bit.ly/dOCNQQ
Thanks in advance!
$(function(){
$('#CCinfo').hide();
$('#paymethod').change(function(){
var payme = $(this).val();
if (payme=='CreditCard') {
$('#CCinfo').show("slow");
} else {
$('#CCinfo').hide();
}
});
});
<tr>
<td colspan="2" align="right">
<strong>pay by:</strong><br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="CreditCard"/>credit card<br />
<tr>
<td colspan="2" align="right"><strong>pay by:</strong><br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="CreditCard"/>credit card<br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="Check"/>cash<br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="At Dinner"/>check
</td>
</tr>
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="Check"/>cash<br />
<input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="At Dinner"/> check
</td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的示例中,多个输入具有相同的 id“paymethod”,然后您可以使用它来侦听
change
事件。在 HTML 中,元素的 id 属性必须是唯一的,当您调用$("#paymethod")
时,它只会查找具有该 id 的第一个输入。您应该删除公共 id 属性或创建唯一的 id 属性。您应该使用name
属性来定位无线电输入。所以 HTML 会是这样的:
获取 radio 元素的 JS 会是:
In your example multiple inputs have the same id, "paymethod", which you then use to listen for a
change
event. In HTML the id attribute of an element must be unique, when you call$("#paymethod")
it is only finding the first input with this id. You should either remove the common id attribute or create unique ones. You should use thename
attribute to locate the radio inputs.So the HTML would be something like this:
The JS to get the radio elements would be:
所有单选按钮都有相同的 ID。当您选择
$('#paymethod').val()
时,您仅获得第一个单选按钮的值。巧合的是,这始终是信用卡。尝试:
All of your radio buttons have the same ID. When you select
$('#paymethod').val()
, you only get the value of the first radio button. Coincidentally, this is always the credit card.Try:
我看了你的代码。多个输入元素不能具有相同的 ID。代码中发生这种情况的原因是因为点击处理程序仅适用于 ID 作为 paymethod 的第一个元素,而不适用于其他两个元素。只需为所有这些使用一个公共类,并将单击处理程序绑定到该类即可。
I had a look at your code. You can't have multiple input elements with the same ID. The reason why this is happening in your code is because the click handler is working for only the first element with ID as paymethod, not the other two. Just use a common class for all of them and bind a click handler to that class.