简体   繁体   English

检查表单数据值并禁用提交按钮jQuery表单

[英]Check for form data values and disable submit button jQuery form

I'm trying to disable to submit button inorder to avoid duplicate entries. 我正在尝试禁用提交按钮,以避免重复输入。

I have checked all SOF, Google and so. 我已经检查了所有SOF,Google等。

What happens is, I get the code for disabling the submit button but it doesn't check for whether the form is completed with data or not. 发生的是,我获得了禁用“提交”按钮的代码,但是它不检查表单是否用数据完成。

To minimize your time and effort I have the code here is JSFiddle. 为了减少您的时间和精力,我在这里使用的代码是JSFiddle。 http://jsfiddle.net/4gL0f2tz/ http://jsfiddle.net/4gL0f2tz/

What I'm trying to do is. 我想做的是。

  1. Check whether the form values are completed (if not throw error - for this I use required attribute) 检查表单值是否完成(如果没有抛出错误,为此,我使用required属性)
  2. Once the form is completed with values then disable the submit button and proceed to process page. 填写完表单的值后,请禁用提交按钮,然后继续处理页面。

Here is the jQuery snippet 这是jQuery片段

    <script type="text/javascript">
    $(document).ready(function(){
    $('input:submit').click(function(){
            $('p').text("Form submiting.....");
            $('input:submit').attr("disabled", true);   
            $.ajax({
            type: "POST",
            url: "form_process.php",
            data: "id=abc" ,
            success: function(){alert('success');}
            });
    });
    });
    </script>

The full code is available here. 完整代码可在此处获得。 http://jsfiddle.net/4gL0f2tz/ http://jsfiddle.net/4gL0f2tz/

Note: I have code to disable the submit button, how can I disable after checking whether all the form values are completed and then disable it. 注意:我有禁用提交按钮的代码,在检查所有表单值是否均已完成然后禁用之后,如何禁用它。

i've updated your fiddle to this: click here 我已将您的小提琴更新为: 单击此处

you can just check each required field, whether its filled (or whatever you want) and then disable the button. 您只需检查每个必填字段,是否已填满(或您想要的任何内容),然后禁用该按钮即可。

the "magic" is just simply this: “魔术”就是这样:

$('input:submit').click(function(evt){

// We think any required field is filled
var filled = true;
$('input[required]:visible').each(function(key, el) {
    // Check if a field is not filled (just an example)
    // you could also do any other checks
    if ( $(el).val() == '' ) filled = false;
});

if ( filled == true ) { /* send it */ }

evt.preventDefault();

});

but you need to stop the form from being send, because you want to send your form by AJAX request... you can do this with preventDefault() 但是您需要停止发送表单,因为您想通过AJAX请求发送表单...您可以使用preventDefault()进行此操作

You would put this in your .submit() function instead of a .click() function: 您可以将其放在.submit()函数中,而不是.submit() .click()函数中:

<script type="text/javascript">
$(document).ready(function(){
$('form').submit(function(e){
        e.preventDefault();

        //validate
        $(":input",$(this)).each(function(i,v){
           if(v.val().trim()==""){ alert("Fix empty field!"); return false; }
        });

        $('p').text("Form submiting.....");
        $('input:submit', $(this)).attr("disabled", true);   
        $.ajax({
        type: "POST",
        url: "form_process.php",
        data: "id=abc" ,
        success: function(){alert('success');
        $('input:submit', $(this)).removeAttr("disabled");   }
        });
});
});
</script>

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

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