否则禁用表单按钮

否则禁用表单按钮

本文介绍了除非填写所有文本输入字段,否则禁用表单按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个文本输入的表单,我不想为每个表单添加id,因为它们是从服务器端代码生成的 - 字段数可能不同等等。我只是想要能够禁用提交按钮,直到在每个文本输入中输入文本。

I have a form that has multiple text inputs, I don't want to add id to each one as they are generated from server side code - number of fields may differ etc. I just want to be able to disable the submit button until there is text entered into each text input.

我已经做到这一点,但只是禁用按钮,直到文本输入到一个文本输入字段 - 我希望它保持禁用状态,直到文本输入到所有文字输入。

I have gotten this far, but only disables button until text entered in to one text input field - I want it to stay disabled until text entered in to all text inputs.

    <script>
        $(function () {
            $('#button').attr('disabled', true);

            $('input:text').keyup(function () {
                $('#button').prop('disabled', this.value == "" ? true : false);
            })
        });
    </script>

我还试过 $('input:text')。每个( ).keyup(function(){ - 但不能使按钮可点击?

I have also tried $('input:text').each().keyup(function (){ - but does not make button clickable?

推荐答案

$('#button').attr('disabled', true);
$('input:text').keyup(function () {
   var disable = false;
       $('input:text').each(function(){
            if($(this).val()==""){
                 disable = true;
            }
       });
  $('#button').prop('disabled', disable);
});

Demo

这篇关于除非填写所有文本输入字段,否则禁用表单按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:04