jquery 隐藏仅有效一次

发布于 2024-10-20 21:33:19 字数 1533 浏览 8 评论 0原文

我有一个最初隐藏的区域,应该根据字段的内容出现/消失。

因此,如果用户选择通过信用卡付款,那么额外的字段就会打开,如果没有,那么它们就会消失。

问题是它只能显示它们而不是隐藏它们。

有人可以看这里: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 />&nbsp;
    <input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="Check"/>cash<br />&nbsp;
    <input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="At Dinner"/>check
   </td>
  </tr>
  &nbsp;
  <input type="radio" name="paymethod" id="paymethod" title="*" class="required" value="Check"/>cash<br />&nbsp;
  <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

郁金香雨 2024-10-27 21:33:19

在您的示例中,多个输入具有相同的 id“paymethod”,然后您可以使用它来侦听 change 事件。在 HTML 中,元素的 id 属性必须是唯一的,当您调用 $("#paymethod") 时,它只会查找具有该 id 的第一个输入。您应该删除公共 id 属性或创建唯一的 id 属性。您应该使用 name 属性来定位无线电输入。

所以 HTML 会是这样的:

<td align="right" colspan="2"><strong>pay by:</strong><br>
    <input type="radio" value="CreditCard" class="required" title="*" name="paymethod">credit card<br> 
    <input type="radio" value="Check" class="required" title="*" name="paymethod">cash<br> 
    <input type="radio" value="At Dinner" class="required" title="*" name="paymethod"> check
</td>

获取 radio 元素的 JS 会是:

$("input[name='paymethod']").change(function() { ... });

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 the name attribute to locate the radio inputs.

So the HTML would be something like this:

<td align="right" colspan="2"><strong>pay by:</strong><br>
    <input type="radio" value="CreditCard" class="required" title="*" name="paymethod">credit card<br> 
    <input type="radio" value="Check" class="required" title="*" name="paymethod">cash<br> 
    <input type="radio" value="At Dinner" class="required" title="*" name="paymethod"> check
</td>

The JS to get the radio elements would be:

$("input[name='paymethod']").change(function() { ... });
千寻… 2024-10-27 21:33:19

所有单选按钮都有相同的 ID。当您选择 $('#paymethod').val() 时,您仅获得第一个单选按钮的值。巧合的是,这始终是信用卡。

尝试:

$('input[name="paymethod"]').click(function (e) {
    if (e.target.value == 'CreditCard') {
        $('#CCinfo').show("slow");
    } else {
        $('#CCinfo').stop().hide();
    }
});

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:

$('input[name="paymethod"]').click(function (e) {
    if (e.target.value == 'CreditCard') {
        $('#CCinfo').show("slow");
    } else {
        $('#CCinfo').stop().hide();
    }
});
少女情怀诗 2024-10-27 21:33:19

我看了你的代码。多个输入元素不能具有相同的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文