试图让jquery验证器处理特定的按钮点击

时间:2014-07-18 14:54:37

标签: javascript jquery jquery-validate

我试图让jquery验证器在特定的提交按钮上运行而不是在所有按钮上运行。

 <form id="incidentform" action="/emplincidata.php" method="get">  
    <input type="submit" class="button" name="updateincidentButton" value="Update Incident"/>
    <input type="button" class="button" name="cancelincidentButton" value="Cancel Incident"/>
    <br />
    <div class="makescroll" id="bysuper">
        <div class="incident">
            <label class="title">TO BE COMPLETED BY THE SUPERVISOR</label><br />
            <div>
                <label class="eighth" for="incidate">Incident Date</label>
                <input type="text" id="incidate" name="incidate" class="datepick" />
                <label class="eighth" for="incidtime">Incident Time</label>
                <input type="text" id="incidtime" name="incidtime" class="timeinput" />
                <label class="eighth" for="shift">Shift</label>
                <select id="shift" name="shift">  
                    <option selected="selected"></option>
                    <option>Day</option>
                    <option>Evening</option>
                    <option>Night</option>
                </select>                        
            </div>
        </div>
    </div>
</form> 

这是javascript。当我提交被提交时,此代码不起作用。我也试过点击这里失败了。

$(function(){
   $("#updateincidentButton").submit(function(){
       $("form").validate({
        rules: {
            incidate: "required",
            incitime: "required",
            shift:  "required"
        }
    })   
 })
})  

3 个答案:

答案 0 :(得分:0)

您尚未向Id selector提供submit button。所以请给Id selector如下:

<input type="submit" class="button" id="updateincidentButton" name="updateincidentButton" value="Update Incident"/>

答案 1 :(得分:0)

请尝试如下所示: -

$(document).ready(function(){

/* Bind Validate() to Form */

$("#incidentform").validate({
        rules: {
            incidate: "required",
            incitime: "required",
            shift:  "required"
        }
    })  


/* Bind Click() to Button*/

        $("input[name=updateincidentButton]").click(function () {

            if ($(#incidentform).valid()) {
                // post stuffs
            }
            return false;
        });

});

答案 2 :(得分:0)

试试这个:

$(document).ready(function(){

/* Bind Validate() to Form */

    $("#incidentform").validate({
            rules: {
                incidate: "required",
                incitime: "required",
                shift:  "required"
            },
            submitHandler :function() {
            // DO STUFF HERE WHEN EVERYTHING IS VALIDATED e.g submit form
                alert('VALID');
            }


        })  

});

小提琴:http://jsfiddle.net/ZKge8/ 只需滚动到页面底部即可跳过验证插件脚本。