简体   繁体   English

如何使用jQuery在laravel中验证表单时禁用按钮

[英]How to dissable a button while validating a form in laravel using jquery

I want my Submit button disable until the elements in the form are not filled..! 我希望禁用“提交”按钮,直到表单中的元素没有填写。

I am using Laravel 5.4 我正在使用Laravel 5.4

There are only 3 fields out of which one is textbox and another is email and another is a select with options. 只有3个字段,其中一个是文本框,另一个是电子邮件,另一个是带有选项的选择。 Here is my Form..! 这是我的表格..!

{{ Form::open(array('url' => 'student', 'id' => 'frmReg')) }}

<div class="form-group">
    <label>Name:</label>
    <input type="text" name="name" id="name" class="form-control">
    <div id="aName" style="color:red"></div>
</div>

<div class="form-group">
    <label>Email:</label>
    <input type="email" name="email" id="email" class="form-control">
    <div id="aEmail" style="color:red"></div>
</div>

<div class="form-group">
    <label id="gen">Gender:</label>
    <br>
    <select name="gender">
        <option value="Male">Male</option>
        <option value="Female">Female</option>
    </select>

</div>
<button class="btn btn-primary" id="sub">Create a student</button>
{{ Form::close() }}

Here is my jQuery script 这是我的jQuery脚本

<script>
var isValid=0;
    $(document).ready(function(){
       $("#frmReg").validate({
            rules: {
                name: {
                    required: true
                },
                email: {
                    required: true
                }
            },
            messages: {
                name: "Name is Empty",
                email: "Enter a valid Email ID..!"
            },
            submitHandler: function(form) { // <- pass 'form' argument in
                $("#sub").attr("disabled", true);
                form.submit(); // <- use 'form' argument here.
            }
        });
});
</script>

Just look at this code and place it where you are submiting your form dont use document.ready function. 只需查看此代码,然后将其放在提交表单的位置即可,不要使用document.ready函数。

$(document).on('click','#youBtnIdHere',function(){
 $("#frmReg").validate({
            rules: {
                name: {
                    required: true
                },
                email: {
                    required: true
                }
            },
            messages: {
                name: "Name is Empty",
                email: "Enter a valid Email ID..!"
            },
            submitHandler: `enter code here`function() { 
                $("#sub").attr("disabled", true);
                 $("#frmReg").submit(); 
            }
        }); });`

OR you can also set in your blad code like this way 或者您也可以像这样设置您的代码

    {{ Form::open(array('url' => 'student', 'i`enter code here`d' => 'frmReg','onsubmit' => '$("#yourBtnId").prop("disabled",true)')) }}

Hope this will help you 希望这个能对您有所帮助

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

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