启用提交按钮如果至少有一个字段填写了值。

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

Enable submit button if atleast one field has filled value

问题

抱歉,这可能是一个重复的问题,但我无法轻松找到一个直接的解决方案。

我已经查看了 Stack Overflow 上的答案,并发现,如果所有字段都填写了,则启用 submit

$(document).ready(function() {
    $('.field input').keyup(function() {

        var empty = false;
        $('.field input').each(function() {
            if ($(this).val().length == 0) {
                empty = true;
            }
        });

        if (empty) {
            $('.actions input').attr('disabled', 'disabled');
        } else {
            $('.actions input').attr('disabled', false);
        }
    });
});
<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='actions'>
      <input type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>

但如果至少有一个字段非空,如何实现相同的功能(启用提交按钮)?

英文:

Sorry this might be a duplicate Q , but could not find a direct solution easily,

I have gone through SO answer and found , enable submit if all values are filled ,

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

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

$(document).ready(function() {
    $(&#39;.field input&#39;).keyup(function() {

        var empty = false;
        $(&#39;.field input&#39;).each(function() {
            if ($(this).val().length == 0) {
                empty = true;
            }
        });

        if (empty) {
            $(&#39;.actions input&#39;).attr(&#39;disabled&#39;, &#39;disabled&#39;);
        } else {
            $(&#39;.actions input&#39;).attr(&#39;disabled&#39;, false);
        }
    });
});

<!-- 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;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 -->

But how to achieve the same(enable submit) if atleast one field is non-empty

答案1

得分: 4

你可以尝试这样做:

$(document).ready(function() {
    $('.field input').keyup(function() {
        var hasValue = $('#username,#password').filter((index, input) => 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='actions'>
      <input type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>

更新:

$('#username,#password').filter((index, input) => input.value.length > 0).length

这行代码的意思是我们将检查两个输入元素 usernamepassword 的值,如果它们都没有任何值,按钮将被禁用。如果你想在启用/禁用之前检查另一个输入元素,你可以添加:

$('#username,#password,#input_3,#input_4').filter(...).length

这样,只有当 usernamepasswordinput_3input_4 元素都没有值时,按钮才会被禁用。确保每个输入元素都有一个特定的 id。

更新 2:

检查 usernamepassword 是否都有值:

var hasValue = $('#username,#password').filter((index, input) => input.value.length > 0).length === 2

检查 fieldfield4field5 是否都有值:

var hasValue = $('#field,#field4,#field5').filter((index, input) => input.value.length > 0).length === 3
英文:

You can try this:

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

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

$(document).ready(function() {
    $(&#39;.field input&#39;).keyup(function() {
        var hasValue = $(&#39;#username,#password&#39;).filter((index, input) =&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;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 -->


Update:

$(&#39;#username,#password&#39;).filter((index, input) =&gt; input.value.length &gt; 0).length

This line means we will check the value of the 2 input elements username and password, if all of them don't have any value, the button will be disabled. If you want to check another input element before enabling/disabling, you can add:

$(&#39;#username,#password,#input_3,#input_4&#39;).filter(...).length

By this way, the button will be disabled when username, password, input_3 and input_4 elements don't have any value. Make sure you have a specific id for each input element.


Update 2:

Checking for both username and password have some value:

var hasValue = $(&#39;#username,#password&#39;).filter((index, input) =&gt; input.value.length &gt; 0).length === 2

Checking for field, field4 and field5 have some value:

var hasValue = $(&#39;#field,#field4,#field5&#39;).filter((index, input) =&gt; input.value.length &gt; 0).length === 3

答案2

得分: 1

以下是您要翻译的内容:

You could with Jquery.map and Array.find

  1. Jquery.map().get() is get all the input value length with condition.

    1. Jquery.map().get()用于根据条件获取所有输入值的长度。
  2. Array.find will find the any one of the input as valid length of string.
    2. Array.find会找到任何一个输入作为有效的字符串长度。

  3. Then finally the result will be reversely compare prop('disabled', !filled).
    3. 最后,结果将被反向比较prop('disabled', !filled)

Updated with select option

Required field based. Button only enabled after required field 1 and 3 both are filled.
使用选择选项进行更新

基于必填字段。仅在必填字段1和3都填写后才启用按钮。

英文:

You could with Jquery.map and Array.find

  1. Jquery.map().get() is get all the input value length with condition.
  2. Array.find will find the any one of the input as valid length of string
  3. Then finally the result will be reversely compare prop(&#39;disabled&#39;, !filled)

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

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

$(document).ready(function() {
  $(&#39;.field input&#39;).keyup(function() {
    var res = $(&#39;.field input&#39;).map(function() {
        return $(this).val().length != 0
    }).get()
    var filled  = res.find(a=&gt; a == true)
    $(&#39;.actions input&#39;).prop(&#39;disabled&#39;, !filled);
  });
});

<!-- 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;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 -->

Updated with select option

Required field based .Button only enabled after required field 1 and 3 both are filled

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

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

$(document).ready(function() {
  $(&#39;.field input,.field select&#39;).on(&#39;keyup change&#39;,function() {
    var res = $(&#39;.field input[required],.field select[required]&#39;).map(function() {
      return $(this).val().trim().length != 0
    }).get();
    var filled = res.every(a =&gt; a == true)
    $(&#39;.actions input&#39;).prop(&#39;disabled&#39;, !filled);
  });
});

<!-- 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; required 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;label for=&quot;password&quot;&gt;email *&lt;/label&gt;
      &lt;input id=&quot;password&quot; required type=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&#39;field&#39;&gt;
      &lt;label for=&quot;password&quot;&gt;select email *&lt;/label&gt;
      &lt;select required&gt;
        &lt;option value=&quot;&quot;&gt;Select&lt;/option&gt;
        &lt;option value=&quot;1&quot;&gt;one&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;two&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

使用jQuery的props方法。

$(document).ready(function() {
    $(':input[type="submit"]').prop('disabled', true);
    $('input').keyup(function() {
        if ($(this).val() != '') {
            $(':input[type="submit"]').prop('disabled', false);
        } else {
            $(':input[type="submit"]').prop('disabled', true);
        }
    });
});
<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='actions'>
            <input type="submit" value="Login" disabled="disabled" />
        </div>
    </form>
</div>
英文:

Use jQuery props method.

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

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

$(document).ready(function() {
     $(&#39;:input[type=&quot;submit&quot;]&#39;).prop(&#39;disabled&#39;, true);
     $(&#39;input&#39;).keyup(function() {
        if($(this).val() != &#39;&#39;) {
           $(&#39;:input[type=&quot;submit&quot;]&#39;).prop(&#39;disabled&#39;, false);
        }else{
          $(&#39;:input[type=&quot;submit&quot;]&#39;).prop(&#39;disabled&#39;, true);
        }
     });
 });

<!-- 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;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 -->

答案4

得分: 1

另一种处理方法是:

存储字段,并在每次按键时创建一个新的字段集合,通过其值对每个字段进行过滤。

如果空字段的数量与所有字段的数量相同,则禁用按钮:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
    var $fields = $('.field input');

    $fields.keyup(function() {

        var $emptyFields = $fields.filter(function(){
            return !this.value;
        });

        $('.actions input').prop('disabled', $emptyFields.length === $fields.length);
    });
});
<!-- 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='actions'>
      <input type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>
<!-- end snippet -->

文档:

英文:

Another way to go about it:

Store the fields, and create a new collection of them each time a key is pressed, filtering each one by its value.

If the length of our empty fields is the same as the length of all fields, disable the button:

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

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

$(document).ready(function() {
    var $fields = $(&#39;.field input&#39;);
    
    $fields.keyup(function() {

        var $emptyFields = $fields.filter(function(){
            return !this.value;
        });
        
        $(&#39;.actions input&#39;).prop(&#39;disabled&#39;, $emptyFields.length === $fields.length);
    });
});

<!-- 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;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 -->

Documentation:

答案5

得分: 0

以下是翻译好的代码部分:

var filled = false;
$('.field input').each(function() {
    if ($(this).val().length > 0) {
        filled = true;
        return;     // 仅添加 return 意味着在任何字段填充时退出循环。
    }
});

if (filled) {
    $('.actions input').attr('disabled', false);
} else {
    $('.actions input').attr('disabled', 'disabled');
}

请注意,代码中的HTML实体(如&#39;)在翻译时保持不变。

英文:

You can try this in a simple way, as shown below:

var filled = false;
$(&#39;.field input&#39;).each(function() {
            if ($(this).val().length &gt; 0) {
                filled = true;
                return;     // just add return means break from loop whenever any field is filled. 
            }
        });

        if (filled) {
            $(&#39;.actions input&#39;).attr(&#39;disabled&#39;, false);
        } else {
            $(&#39;.actions input&#39;).attr(&#39;disabled&#39;, &#39;disabled&#39;);
        }
    });

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

发表评论

匿名网友

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

确定