简体   繁体   English

输入字段为空时禁用提交按钮

[英]disable submit button when input fields is null

I want to disable submit button when 4 input fields are nulls 当4个输入字段为空时,我想禁用提交按钮

<input type="text" name="val1" id="val1">
<input type="text" name="val2" id="val2">
<input type="text" name="val3" id="val3">
<input type="text" name="val4" id="val4">

submit button 提交按钮

<input name="contact" type="submit" id="contact" value="Update" style="padding:5px" class="sendButton" disabled />

I am using 我在用

            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
            $('.sendButton').attr('disabled',true);

            $('#val1').keyup(function(){
                if($(this).val().length !=0){
                    $('.sendButton').attr('disabled', false);
                }
                else
                {
                    $('.sendButton').attr('disabled', true);        
                }
            })
            $('#val2').keyup(function(){
                if($(this).val().length !=0){
                    $('.sendButton').attr('disabled', false);
                }
                else
                {
                    $('.sendButton').attr('disabled', true);        
                }
            })
            $('#val3').keyup(function(){
                if($(this).val().length !=0){
                    $('.sendButton').attr('disabled', false);
                }
                else
                {
                    $('.sendButton').attr('disabled', true);        
                }
            })
            $('#val4').keyup(function(){
                if($(this).val().length !=0){
                    $('.sendButton').attr('disabled', false);
                }
                else
                {
                    $('.sendButton').attr('disabled', true);        
                }
            })
        });
        </script>

I think I am using wrongly. 我想我用错了。 I put all ids in one function. 我将所有ID放在一个函数中。

 $('#val1','#val2','#val3','#val4').keyup(function(){

this code is not working . 此代码不起作用。 any suggestion. 任何建议。 how to use & and condition(val1 &val2 & val3 &val4) in these function?. 在这些函数中如何使用&和condition(val1&val2&val3&val4)?

The way I would try and solve this is like this: 我尝试解决此问题的方式如下:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">

    $("#val1").keyup(function(event) {
        validateInputs();
    });

    $("#val2").keyup(function(event) {
        validateInputs();
    });

    $("#val3").keyup(function(event) {
        validateInputs();
    });

    $("#val4").keyup(function(event) {
        validateInputs();
    });

    function validateInputs(){
        var disableButton = false;

        var val1 = $("#val1").val();
        var val2 = $("#val2").val();
        var val3 = $("#val3").val();
        var val4 = $("#val4").val();

        if(val1.length == 0 || val2.length == 0 || val3.length == 0 || val4.length == 0)
            disableButton = true;

        $('.sendButton').attr('disabled', disableButton);
    }
</script>

This way you will have a single place for checking your logic and not having to maintain it at N number of places if you diced to add more inputs later. 这样,您将拥有一个检查逻辑的地方,并且如果以后打算增加更多的输入,则不必将其维持在N个地方。 And also a bit better solution would be to give your inputs the same class so you could do something like 还有一个更好的解决方案是为您的输入提供相同的类,以便您可以执行类似的操作

$(".myInputs").keyup(function(event){
    validateInputs();
});

Here is a jsFiddle example: https://jsfiddle.net/moj2dnup/ 这是一个jsFiddle示例: https ://jsfiddle.net/moj2dnup/

You could use the pattern attribute you would also need to use the required attribute otherwise an input field with an empty value will be excluded from constraint validation . 您可以使用pattern属性 ,也需要使用required属性,否则约束验证将排除具有空值的输入字段。 This will prevent your form from being submitted, however, it won't disable your button. 这将阻止您提交表单,但是,它不会禁用您的按钮。

Example: 例:

<input pattern=".{5,}"   required title="5 character minimum">
<input pattern=".{5,10}" required title="between 5 and 10 characters">

 $(document).ready(function(){ $('.sendButton').attr('disabled',true); $('.txtValues').keyup(function(){ var checkNull=false; $('.txtValues').each(function(index,input){ if($(this).val().length !=0)checkNull=true; }); if(checkNull){ $('.sendButton').attr('disabled', false); }else{ $('.sendButton').attr('disabled', true); } }); }); 
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <input type="text" name="val1" id="val1" class="txtValues"> <input type="text" name="val2" id="val2" class="txtValues"> <input type="text" name="val3" id="val3" class="txtValues"> <input type="text" name="val4" id="val4" class="txtValues"> <input name="contact" type="submit" id="contact" value="Update" style="padding:5px" class="sendButton" disabled /> 

Use a common css class for all inputs. 对所有输入使用通用的CSS类。 Loop through inputs using $.each and check all values are null. 使用$ .each遍历输入,并检查所有值是否为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM