启用提交按钮如果至少有一个字段填写了值或选择了一个下拉选项。

huangapple go评论64阅读模式
英文:

Enable submit button if atleast one field has filled value / or a selected dropdown

问题

I have a <select> tag along with <input>, as below, but it doesn't work.

如下所示,我有一个包含<select><input>的标签,但它不起作用。

How can I enable the submit button if at least one field has a value or a selected option? How can I enable the submit button on multiple (and/& in general) fields selection?

如果至少有一个字段具有值或已选择的选项,如何启用提交按钮?如何在选择多个(通常是and/&)字段时启用提交按钮?

Any help is appreciated!

感谢任何帮助!

$(document).ready(function() {
  $('.field input,.field select').keyup(function() {
    var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;

    $('.actions input').attr('disabled', hasValue ? false : 'disabled');
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
  <form>
    <div class='field'>
      <label for="username">Username</label>
      <input id="username" type="text" />
    </div>
    <div class='field'>
      <label for="password">Password</label>
      <input id="password" type="password" />
    </div>
    <div class='field'>
      <select id="position">
        <option value="">position</option>
        <option value="1">first</option>
        <option value="2">second</option>
        <option value="3">third</option>
      </select>
    </div>
    <div class='actions'>
      <input type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>
英文:

I have a &lt;select&gt; tag along with &lt;input&gt;, as below, but it doesn't work.

How can I enable the submit button if at least one field has a value or a selected option? How can I enable the submit button on multiple (and/&amp; in general) fields selection?

Any help is appreciated!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&#39;.field input,.field select&#39;).keyup(function() {
    var hasValue = $(&#39;#username,#password,#position&#39;).filter((index, input, select) =&gt; input.value.length &gt; 0).length;

    $(&#39;.actions input&#39;).attr(&#39;disabled&#39;, hasValue ? false : &#39;disabled&#39;);
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&#39;form&#39;&gt;
  &lt;form&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;
      &lt;input id=&quot;username&quot; type=&quot;text&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
      &lt;input id=&quot;password&quot; type=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;select id=&quot;position&quot;&gt;
        &lt;option value=&quot;&quot;&gt;position&lt;/option&gt;
        &lt;option value=&quot;1&quot;&gt;first&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;second&lt;/option&gt;
        &lt;option value=&quot;3&quot;&gt;third&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
    &lt;div class=&#39;actions&#39;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Login&quot; disabled=&quot;disabled&quot; /&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 3

我会在form上使用inputchange事件,而不是在字段上使用keyup事件,因为当然可以使用鼠标选择内容。我还会使用prop而不是attr来设置disabled属性:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
$(document).ready(function() {
  $('form').on("input change", function() {
    var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;

    $('.actions input').prop('disabled', !hasValue);
  });
});

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
  <form>
    <div class='field'>
      <label for="username">Username</label>
      <input id="username" type="text" />
    </div>
    <div class='field'>
      <label for="password">Password</label>
      <input id="password" type="password" />
    </div>
    <div class='field'>
      <select id="position">
        <option value="">position</option>
        <option value="1">first</option>
        <option value="2">second</option>
        <option value="3">third</option>
      </select>
    </div>
    <div class='actions'>
      <input type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>

<!-- end snippet -->

另外,我认为在选择要检查的字段时可能会使用更通用的选择器,并且在知道需要启用表单时停止检查:

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->
$(document).ready(function() {
  $('form').on("input change", function() {
    var hasValue = false;
    $(this).find("input[type=text], input[type=password], select").each(function() {
      if (this.value) {
        hasValue = true;
        return false;
      }
    });
    $(this).find('.actions input').prop('disabled', !hasValue);
  });
});

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
  <form>
    <div class='field'>
      <label for="username">Username</label>
      <input id="username" type="text" />
    </div>
    <div class='field'>
      <label for="password">Password</label>
      <input id="password" type="password" />
    </div>
    <div class='field'>
      <select id="position">
        <option value="">position</option>
        <option value="1">first</option>
        <option value="2">second</option>
        <option value="3">third</option>
      </select>
    </div>
    <div class='actions'>
      <input type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>

<!-- end snippet -->
英文:

I would use input and change on the form, rather than keyup on the fields, since of course you can pick things with the mouse. I'd also use prop, not attr, to set disabled:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&#39;form&#39;).on(&quot;input change&quot;, function() {
    var hasValue = $(&#39;#username,#password,#position&#39;).filter((index, input, select) =&gt; input.value.length &gt; 0).length;

    $(&#39;.actions input&#39;).prop(&#39;disabled&#39;, !hasValue);
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&#39;form&#39;&gt;
  &lt;form&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;
      &lt;input id=&quot;username&quot; type=&quot;text&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
      &lt;input id=&quot;password&quot; type=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;select id=&quot;position&quot;&gt;
        &lt;option value=&quot;&quot;&gt;position&lt;/option&gt;
        &lt;option value=&quot;1&quot;&gt;first&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;second&lt;/option&gt;
        &lt;option value=&quot;3&quot;&gt;third&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
    &lt;div class=&#39;actions&#39;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Login&quot; disabled=&quot;disabled&quot; /&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

Separately, I think I'd probably use a more general selector when selecting the fields to check, and stop the check as soon as I know I need to enable the form:

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&#39;form&#39;).on(&quot;input change&quot;, function() {
    var hasValue = false;
    $(this).find(&quot;input[type=text], input[type=password], select&quot;).each(function() {
      if (this.value) {
        hasValue = true;
        return false;
      }
    });
    $(this).find(&#39;.actions input&#39;).prop(&#39;disabled&#39;, !hasValue);
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&#39;form&#39;&gt;
  &lt;form&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;
      &lt;input id=&quot;username&quot; type=&quot;text&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
      &lt;input id=&quot;password&quot; type=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;select id=&quot;position&quot;&gt;
        &lt;option value=&quot;&quot;&gt;position&lt;/option&gt;
        &lt;option value=&quot;1&quot;&gt;first&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;second&lt;/option&gt;
        &lt;option value=&quot;3&quot;&gt;third&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
    &lt;div class=&#39;actions&#39;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Login&quot; disabled=&quot;disabled&quot; /&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

您可以使用 change 事件而不是 keyup 事件。

$(document).ready(function() {
  $('.field input,.field select').change(function() {
    var hasValue = $('#username,#password,#position').filter((index, input, select) => input.value.length > 0).length;

    $('.actions input').attr('disabled', hasValue ? false : 'disabled');
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
  <form>
    <div class='field'>
      <label for="username">Username</label>
      <input id="username" type="text" />
    </div>
    <div class='field'>
      <label for="password">Password</label>
      <input id="password" type="password" />
    </div>
    <div class='field'>
      <select id="position">
        <option value="">position</option>
        <option value="1">first</option>
        <option value="2">second</option>
        <option value="3">third</option>
      </select>
    </div>
    <div class='actions'>
      <input type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>
英文:

You can use the change event instead of the keyup

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $( &#39;.field input,.field select&#39; ).change(function() {
   var hasValue = $(&#39;#username,#password,#position&#39;).filter((index, input, select) =&gt; input.value.length &gt; 0).length;

    $(&#39;.actions input&#39;).attr(&#39;disabled&#39;, hasValue ? false : &#39;disabled&#39;);
});
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&#39;form&#39;&gt;
  &lt;form&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;
      &lt;input id=&quot;username&quot; type=&quot;text&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
      &lt;input id=&quot;password&quot; type=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;select id=&quot;position&quot;&gt;
        &lt;option value=&quot;&quot;&gt;position&lt;/option&gt;
        &lt;option value=&quot;1&quot;&gt;first&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;second&lt;/option&gt;
        &lt;option value=&quot;3&quot;&gt;third&lt;/option&gt;
      &lt;/select&gt;
    &lt;/div&gt;
    &lt;div class=&#39;actions&#39;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Login&quot; disabled=&quot;disabled&quot; /&gt;
    &lt;/div&gt;
  &lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 1

你只需使用 change 而不是 keyup

$(document).ready(function() {
  $('.field input,.field select').change(function() {
    var hasValue = $('#username,#password,#position').filter((index, input ,select) => input.value.length > 0).length;

    $('.actions input').attr('disabled', hasValue ? false : 'disabled');
  });
});
英文:

You just need to use change rather than keyup

$(document).ready(function() {
  $(&#39;.field input,.field select&#39;).change(function() {
    var hasValue = $(&#39;#username,#password,#position&#39;).filter((index, input ,select) =&gt; input.value.length &gt; 0).length;

    $(&#39;.actions input&#39;).attr(&#39;disabled&#39;, hasValue ? false : &#39;disabled&#39;);
  });
});

huangapple
  • 本文由 发表于 2020年1月6日 17:36:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定