jquery 验证禁用按钮,直到输入有效

泛海

您好,我有这个表单,允许用户在我的自定义 Web 应用程序中更新他们的详细信息,这是代码:

<g:form class = "updateCustomerClient" controller="customerClient" action="updateCustomerClient" id="${cus.id}">


    <div style="width:100%; padding-top: 50px; margin-right: auto; margin-left: auto; text-align: center">
        <div style = "height: 50vh; width: 50%; margin-left:auto; margin-right:auto;">


            <div>


                %{--                        Table for customer client--}%
                <table class="table table-striped" id="table t1">
                    <colgroup>
                        <col>
                        <col>
                        <col>
                        <col>
                        <col>
                    </colgroup>
                    <thead>

                    <tr>
                        <th colspan="5" style="height: 40px;">
                            <h6 style="font-weight: bold; margin-left: 40px;">
                                <g:link action="index" controller="customer">All Customers</g:link>&nbsp;>>&nbsp;
                                <g:link action="index" controller="customerClient">${customers.name} clients</g:link> &nbsp;>>&nbsp;
                                <a href="#" onclick="goBack()">${cus.clientName}</a>&nbsp;>>&nbsp;EDIT
                            </h6>
                        </th>
                    </tr>

                    <tr>
                        <th colspan="5" style="height: 40px; text-align:center;background-color:lightseagreen"><h6 style="font-weight: bold">EDIT CUSTOMER CLIENT</h6></th>
                    </tr>


                    %{--                                            <tr style="width:100%; height: 20px;">--}%
                    %{--                                                <td colspan="2" style="text-align: right;"> <g:link action="index" controller="customer"><button id = "back" type="button" class="btn btn-primary" style = "font-weight: bold"><i id = "back-icon" class="fa fa-chevron-left" aria-hidden="true"></i>Back</button></g:link></td>--}%
                    %{--                                            </tr>--}%

                    <tr style="width:100%; height: 70px;">
                        <td style = "width:50%; font-weight: bold; text-align: right;padding-right: 20px;">Customer Name:</td>
                        <td style = "width:50%; font-weight: bold; text-align: left;padding-left: 20px;"><label value = "${customers.name}"style = "font-weight: bold;color:lightseagreen;"> ${customers.name}  </label></td>
                    </tr>

                    <td style = "width:50%; font-weight: bold; text-align: left;padding-left: 20px;"><g:hiddenField required ="true" value = "${customers.id}" class="form-control" name="customerId" from="${customers}" optionKey="id" optionValue="name"></g:hiddenField></td>

                    <tr style="width:100%;height: 70px;background-color: #f7fafd; ">
                        %{--                        <div class = "col-md-5 text-right" style = "font-weight: bold"><span class="placeholder">Channel ID</span></div>--}%
                        <td style = "width:50%;font-weight: bold; text-align: right;padding-right: 20px;"><span class="placeholder">Client Name:</span></td>
                        <td style = "width:50%; font-weight: bold; text-align: left;padding-left: 20px;"><input value = "${cus.clientName}" required type="text" style = "width: 70%" class="form-control" id="clientname" placeholder="Enter Client Name" name="clientname" ></td>

                    </tr>

                    <tr style="height: 70px;">
                        <td style = "width:50%;font-weight: bold; text-align: right;padding-right: 20px;">Channel:</td>
                        <td style = "width:50%; font-weight: bold; text-align: left;padding-left: 20px;"><g:select value = "${cus.clientChannel}" required="true" style = "width: 70%" class="form-control" name="clientchannel" from="${["Line","Facebook"]}"></g:select></td>

                    </tr>


                    <tr style="height: 70px;background-color: #f7fafd; ">
                        <td style = "width:50%;font-weight: bold; text-align: right;padding-right: 20px;">Channel ID:</td>
                        <td style = "width:50%; font-weight: bold; text-align: left;padding-left: 20px;"><input value = "${cus.clientChannelId}" required type="text" style = "width: 70%" class="form-control" id="clientchannelid" placeholder="Enter Channel ID" name="clientchannelid" ></td>

                    </tr>

                    
                    <tr style="height: 70px;">
                        <td style = "width:50%;font-weight: bold; text-align: right;padding-right: 20px;">Zendesk API Auth:</td>
                        <td style = "width:50%; font-weight: bold; text-align: left;padding-left: 20px;"><input value = "${cus.zendeskApiAuth}" required style = "width: 70%" class="form-control" id="zendeskApiAuth" placeholder="Enter Zendesk API Auth" name="zendeskApiAuth" ></td>

                    </tr>
                    
                    <tr style="height: 70px;">
                        %{--                                                <g:link><button type="submit" class="btn btn-default confirmation-update" style = "width: 10%;background-color: lightseagreen;color: white;font-weight: bold">Submit</button></g:link>--}%
                        <td colspan="2" style = "width:50%;font-weight: bold; text-align: center;padding-right: 20px;"><g:link><button type="submit" class="btn-disabled btn-reset btn btn-default confirmation-update" disabled = "true" style = "background-color: lightseagreen;color: white;font-weight: bold">Submit</button></g:link></button>
                            <g:link controller = "customerClient" action="showCustomerClient" id = "${cus.id}"><button type="button" class="btn btn-danger" style="font-weight: bold">Cancel</button></g:link></td>
                    </tr>
                    </thead>
                </table>


                %{--                        End of table for customer client--}%
            </div>

        </div>

    </div>
</g:form>

这是 jquery 验证:

    $.validator.addMethod("lettersonly", function(value, element) {
        return this.optional(element) || /^[a-z\s]+$/i.test(value);
    }, "Please provide only alphabet");

    $('.updateCustomerClient').validate({
        rules: {
            clientname: {
                lettersonly: true
            },
            zendeskApiAuth: {
                email:true,
                required:true
            }
        },
        messages:{
            clientname:{
                required:''
            },
            clientchannelid:{
                required:''
            },
      
            zendeskApiAuth:{
                required:''
            }
         
        }
    });

对于提交按钮,我添加了禁用方法,直到输入有效电子邮件:

 <td colspan="2" style = "width:50%;font-weight: bold; text-align: center;padding-right: 20px;"><g:link><button type="submit" class="btn-disabled btn-reset btn btn-default confirmation-update" disabled = "true" style = "background-color: lightseagreen;color: white;font-weight: bold">Submit</button></g:link></button>

这是我在电子邮件有效时删除禁用标签的代码:


    $("#zendeskApiAuth").on("keypress", function(){
        if($(".updateCustomerClient").valid())
        {
            $(".btn-reset").removeAttr("disabled");
        }
    });

一切都按预期工作,但是当我点击该页面时,提交按钮已经禁用,如果用户想要更改其他详细信息而不是 Zendesk Api Auth(电子邮件),他们将无法提交。任何想法我该如何解决这个问题?

泛海

我找到了一个固定的:

 $('.updateCustomerClient input').on('keyup blur', function () { 
        if ($('.updateCustomerClient').valid()) {          
            $('.submit-button').prop('disabled', false);        
        } else {
            $('.submit-button').prop('disabled', 'disabled');  
        }
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带有jquery datepicker错误的jquery验证插件(请输入有效日期)

禁用jQuery提交按钮,直到填写完所有表单输入

禁用按钮单击,直到所有输入字段都包含文本(javascript 和 jquery)

jQuery验证-有效,但不“提交”

jQuery Datepicker有效,但验证消失了

jQuery 验证字段:输入有效信息后,错误消息消失

jQuery验证无法在输入有效电子邮件后消除错误

禁用提交按钮,直到它验证所有数字字段均有效

按钮禁用与jQuery验证

禁用引导程序验证器的提交按钮,直到整个表单都有效

禁用提交按钮,直到选择没有 jquery 的下拉选项

禁用按钮,直到必填字段有效

禁用元素,直到其他输入字段有效

jQuery段验证仅在最初有效

jQuery验证:表单始终有效的问题

Jquery 验证有效,但未提交 ajax

所有jquery验证规则均正常工作(在onkeyup上有效),但仅在onclick提交按钮之后才确认密码验证在工作

jQuery验证:未捕获的TypeError:$(...)。“有效不是函数”,具有有效的引用/顺序

jQuery,在输入键码13(触发按钮)时,这仅在第一次有效,而在之后则无效

jQuery验证和禁用输入字段

隐藏方法有效,但不能在Jquery中禁用

基于输入值启用/禁用 jQuery 按钮?

如何禁用提交按钮,直到表单在角度 12 中有效?

如何禁用聚合物中的按钮,直到字段有效?

禁用提交按钮,直到用户名查找有效

jQuery 验证,在提交时显示有效/无效的表单选项卡

简单的jQuery表单验证并将有效数据发送到其他jsp

如何使用jQuery验证插件以编程方式检查表单是否有效

如何使用jQuery验证插件检查特定字段是否有效?