当前位置: 首页 > 知识库问答 >
问题:

如果所有的输入都不需要填写,如何禁用按钮?

子车征
2023-03-14

我必须在我的表单中禁用按钮,但不是所有的输入都是必需的。在我的代码中,我已经写好了哪些输入需要填充,但仍然必须在表单中填充所有的输入。我该怎么改变呢?

null

$('#billing_first_name, #billing_last_name, #billing_address_1, #billing_postcode, #billing_city, #billing_phone, #billing_email').bind('keyup', function dataFilled() {
  $('form > input').keyup(function() {
    var empty = false;
    $('form > input').each(function() {
      if ($(this).val() == '') {
        empty = true;
      }
    });

    if (empty) {
      $('#place_order').attr('disabled', 'disabled');
    } else {
      $('#place_order').removeAttr('disabled');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  Name<br>
  <input type="text" id="billing_first_name" name="billing_first_name"><br> Surname<br>
  <input type="text" id="billing_last_name" name="billing_last_name"><br> Company's Name (optional)<br>
  <input type="text" id="billing_company" name="billing_company"><br> Street<br />
  <input type="text" id="billing_address_1" name="billing_address_1"><br> Postal code<br>
  <input type="text" id="billing_postcode" name="billing_postcode"><br> City<br>
  <input type="text" id="billing_city" name="billing_city"><br> Phone<br>
  <input type="text" id="billing_phone" name="billing_phone"><br> E-mail<br>
  <input type="text" id="billing_email" name="billing_email"><br>
  <button type="submit" name="place_order" id="place_order" value="Buy" disabled="disabled">Buy</button>
</form>

null

共有1个答案

庾才
2023-03-14

有更好的方法来处理这个问题,但只要对现有代码做最小的更改,就可以预先存储输入:

var requiredFields = $('#billing_first_name, #billing_last_name, #billing_address_1, #billing_postcode, #billing_city, #billing_phone, #billing_email');
requiredFields.on('input', function () {
    var empty = false;
    requiredFields.each(function() {
      if ($(this).val() == '') {
        empty = true;
      }
    });

    if (empty) {
      $('#place_order').attr('disabled', 'disabled');
    } else {
      $('#place_order').removeAttr('disabled');
    }
});

我还删除了会多次添加的内部事件

另一种方法是给您需要的输入一个特定的类,然后使用它,例如:

$('.required').on('input', function () {
    var empty = false;
    $('.required').each(function() {
      if ($(this).val() == '') {
        empty = true;
      }
    });

    if (empty) {
      $('#place_order').attr('disabled', 'disabled');
    } else {
      $('#place_order').removeAttr('disabled');
    }
});
<input type="text" class="required" id="billing_first_name" name="billing_first_name">
 类似资料:
  • 问题内容: 我是React JavaScript的新手。我试图在输入字段为空时禁用按钮。React的最佳方法是什么? 我正在执行以下操作: 这样对吗? 这不仅仅是动态属性的复制,因为我也很好奇要从一个元素到另一个元素传输/检查数据。 问题答案: 您需要将输入的当前值保持在状态中(或通过回调函数或sideways或 _< 此处是您应用的状态管理解决方案>_将其值更改传递给父级,以便最终将其传递回您的

  • Q1:这里的“disabled”是否被认为是一个属性 问题2: 是否所有html禁用按钮都有一个disabled属性,如:

  • 在ts 中禁用... 我只想禁用基于<code>true</code>或<code>false</code>的输入。 我尝试了以下几点:

  • 问题内容: 我想从Go执行perforce命令行“ p4”以执行登录工作。“ p4登录”要求用户输入密码。 如何在Go中运行需要用户输入的程序? 以下代码不起作用。 问题答案: 从os / exec.Command文档: 在执行命令之前,设置命令的Stdin字段。

  • Java 11、Spring Boot 2.1.3、Spring 5.1.5 我有一个Spring Boot项目,其中某些endpoint由API密钥保护。这在目前的代码中运行良好: 这需要一个包含API密钥的头,但仅适用于 到,但它允许客户端连接到我的API,而无需任何证书。 不需要证书的输出示例: 需要证书和api密钥的输出示例: