【问题标题】:jquery disable specific submit buttonjquery禁用特定的提交按钮
【发布时间】:2013-07-04 09:54:38
【问题描述】:

我在一个页面上有几个具有相同类的 div,它们有一个带有输入字段的表单和一个提交按钮。我用 jquery 检查每个输入字段是否为空,并且我希望在该 div 中有一个空字段时禁用该特定 div 中的提交按钮。到此为止:

jQuery(document).ready(function(){
    $('.shipdiv input').each(function(){
    if( $(this).val() == ''){
    $(this).css("background-color","#ff0000");
    $(this).parent('.contact-button').attr('disabled', 'disabled');
}
});
   });

当有一个字段为空时,它会显示为红色,然后我尝试禁用其父级中的提交按钮。但这不起作用。有人知道如何做到这一点吗?

HTML 看起来像这样:

<div class="shipdiv">
<form name="order_002" method="post" action="">
<input type="textfield" class="ship_data" name="bedrijfsnaam"/>
<input type="textfield" class="ship_data" name="straat" />
<input type="textfield" class="ship_data" name="postcode"/>
<input type="submit" name="ship-submit" id="ship-submit" class="contact-button" value="Aanmelden">
</form>
</div>

然后使用其中几个 div。

【问题讨论】:

  • 你能发布一些你的 HTML 吗?
  • 那是用 php 生成的,但我会尝试重新创建它。

标签: jquery


【解决方案1】:

尝试:

 $(this).parent('div').find('.contact-button').attr('disabled', 'disabled');

或使用最近的:

 $(this).closest('div').find('.contact-button').attr('disabled', 'disabled');

【讨论】:

    【解决方案2】:

    可以通过找到父 div,相对于当前输入,然后在 div 中找到按钮来完成。

    $(this).parents('.shipdiv').find('.contact-button').prop('disabled', true);
    

    旁注:.prop('disabled', true) 优于 .attr()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2011-07-23
      • 2015-11-07
      • 1970-01-01
      相关资源
      最近更新 更多