简体   繁体   English

如何使用jQuery验证引导多选下拉列表

[英]How to validate bootstrap multiselect dropdown using jquery

I am trying to validate Bootstrap multiselect dropdown using the code given below, the form is getting validated but i did'nt get the proper output, the validation message is above the selectbox and the message gets multiplied when clicking on the submit button and after selecting option the message did'nt gets removed. 我正在尝试使用下面给出的代码来验证Bootstrap multiselect下拉列表,该表单正在得到验证,但是我没有得到正确的输出,验证消息在selectbox上方,并且在单击Submit按钮和选择后,该消息成倍增加选项消息没有被删除。 Please help to solve my problem. 请帮助解决我的问题。

      <script type="text/javascript">
        $(document).ready(function() {
        $.validator.addMethod("needsSelection", function (value, element) {
            var count = $(element).find('option:selected').length;
            return count > 0;
          });

        $.validator.messages.needsSelection = 'Select Atleast One College'; 

          $("#User").validate({
                  rules: {    
                    'college[]': {
                    needsSelection: true
                    }
                  },
              }
          });

 $("#submit").click(function(e) {
    var isvalidate=$("#User").valid();
    if(isvalidate=="true")
    {

    }
    else
    {
        $("#User").submit();
    }
 });

});

    <form id="User" name="User" method="post" enctype="multipart/form-data" action="index">   
        <select data-placeholder="Choose College Name" name="college[]" class="chosen-select" id="college" multiple tabindex="4">
                    <option value=""></option>
                    <option value="1">abc</option>
                    <option value="2">abc1</option>
        </select>
    <a id="submit">Submit</a>
    </form>

在此处输入图片说明

This is not a direct answer to your question, but may provide a solution to your problem. 这不是您问题的直接答案,但可以为您的问题提供解决方案。

First, change the anchor tag to a button -- because anchor tags have built-in navigation behaviour that you must counter ( event.preventDefault() ) and therefore add needless complexity. 首先,将锚标记更改为按钮-因为锚标记具有内置的导航行为,您必须对它进行抵抗( event.preventDefault() ),因此会增加不必要的复杂性。 How about a button (input or button tags) or a div tag, or ap?) 按钮(输入或按钮标签)或div标签或ap怎么样?)

Next, a simpler structure to evaluating your form: 接下来,一个更简单的结构来评估您的表单:

jsFiddle Demo jsFiddle演示

HTML: (just changed your anchor submit button to: HTML :(只需将锚点提交按钮更改为:

<input id="dosubmit" type="submit" value="Submit" />

Note: do not use "submit" as the id for a submit button, as that is a reserved keyword 注意:请勿将“ submit”用作提交按钮的ID,因为这是保留关键字

javascript/jQuery: javascript / jQuery:

$('#User').submit(function(e){
    var col = $('#college').val();
    if ( col==null || col=='' || col=='Please choose one:' ){
        $('#college').css('background','yellow').focus();
        return false;
    }else{
        $('#college').css('background','transparent');
        alert('Form submitted');
        //continue to submit the form - but not now
        return false;
    }
});

Please consider this verification script for your project. 请为您的项目考虑此验证脚本

This is not an answer to your question but I will advise you and encourage you to use a server-side validation together. 这不是您问题的答案,但我会建议您并鼓励您一起使用服务器端验证。

Your data should be validate and as needed sanitzed on the php file that receives the ajax call. 您的数据应经过验证,并根据需要在接收ajax调用的php文件中进行清理。

If something is going wrong there, the response will always be passed back to your form giving your users the needed feedback. 如果那里出了问题,响应将始终传递回您的表单,为您的用户提供所需的反馈。

You can find many examples on the internet of how to do this. 您可以在互联网上找到许多有关如何执行此操作的示例。

See here an example: https://scotch.io/tutorials/submitting-ajax-forms-with-jquery 请参阅此处的示例: https : //scotch.io/tutorials/submitting-ajax-forms-with-jquery

So to recap: Dont't do client-side validation!! 回顾一下:不要做客户端验证!!

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

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