当我添加jquery验证功能时,我的其他功能停止工作

时间:2014-07-15 16:05:47

标签: javascript jquery jquery-validate

我有一个表单,我有几个javascript / jquery函数正常工作。只要我添加验证功能,它们就会停止工作。我正在使用http://jqueryvalidation.org/中的插件,并将所有函数放在一个js文件中。在添加验证功能之前,所有这些功能都可以正常工作。这是js文件。

$(function(){
        $(".timeinput").timepicker({
          step: 5  
        });
    });

  $(function(){
            $(".datepick").datepicker({
                changeYear: true,
                changeMonth: true
            });
        });


  $(function(){
    $("#incimechtype").change(function(){
        var dropdown = $(this).val();
        $.ajax({
             url:"getinjuryjson.php",
             dataType: "json"  
        }).done( function(data){
             $("#incimech").find("option").remove();
             if(dropdown !== ""){
                $("#incimech").append($('<option/>'));
             }
             switch(dropdown){
                    case "Mechanism":

                        $.each(data, function(key,value){
                            if(value.injmech==='Mechanism'){
                                $("#incimech").append($('<option/>',{
                                value: value.injmechid,
                                text: value.injmechdescrip
                                }));
                                }
                        });
                        break;
                    case "Other":

                        $.each(data, function(key,value){
                            if(value.injmech==='Other'){
                                $("#incimech").append($('<option/>',{
                                value: value.injmechid,
                                text: value.injmechdescrip
                                }));
                                }
                        });
                        break;
                    case "Object":

                        $.each(data, function(key,value){
                            if(value.injmech==='Object'){
                                $("#incimech").append($('<option/>',{
                                value: value.injmechid,
                                text: value.injmechdescrip
                                }));
                                }
                        });
                        break;
                    }

        }
        )
    }
    ) 
  }
  );

    $("#referto").change(function(){
        var rechange = false;
        $('#referto option:selected').each(function(){
            if($(this).text()=="Other"){
                rechange =true;
            }
        });

        if(rechange){
             var textarea = "<textarea name='referother' id='referother' />";
            $("#referto").after(textarea);
        }
        else
        {
            $("#referother").remove();
        } 
    });

  $(function(){
    $("#classcase").change(function(){
        var dropdown = $(this).val();
         $.ajax({
             url:"getoshaclassson.php",
             dataType: "json"  
        }).done( function(data){
             $("#otherrecord").find("option").remove();
             if(dropdown !== ""){
                $("#otherrecord").append($('<option/>'));
             }
             if(dropdown ==="Other Recordable"){
                $.each(data, function(key,value){
                            if(value.oshaclassid > 3){
                                $("#otherrecord").append($('<option/>',{
                                value: value.oshaclassid,
                                text: value.oshaclass
                                }));
                                }
                        });
             }

        }
        )
    }
    ) 
  }
  );


 $(function(){
     $("#addlostdays").click(function(){
     $("#oshadataarea").find("label").remove();
     $("#oshadataarea").find("input").remove();
     $("#oshadataarea").find("textarea").remove();
     $("#oshadataarea").find("br").remove();
    $("#oshadataarea").append($('<label/>',{
        text:"Begin Lost Date",
        for: "newloststartdate",
        class: "eighth"})).append($('<input/>',{
            type: "text",
            id: "newloststartdate",
            name: "newloststartdate",
            class:"datepick"
        })).append($('<label/>',{
        text:"End Lost Date",
        for: "newlostenddate",
        class: "eighth"})).append($('<input/>',{
            type: "text",
            id: "newlostenddate",
            name: "newlostenddate",
            class:"datepick"
        }))
  });
  $('form').on('focus',".datepick", function(){
    $(this).datepicker({
        changeMonth: true,
        changeYear: true
    });
    });
 });



$(function(){
     $("#addjobtrans").click(function(){
     $("#oshadataarea").find("label").remove();
     $("#oshadataarea").find("input").remove();
      $("#oshadataarea").find("textarea").remove();
     $("#oshadataarea").find("br").remove();
    $("#oshadataarea").append($('<label/>',{
        text:"Begin Light Duty",
        for: "newlightstartdate",
        class: "eighth"})).append($('<input/>',{
            type: "text",
            id: "newlightstartdate",
            name: "newlightstartdate",
            class:"datepick"
        })).append($('<label/>',{
        text:"End Light Duty",
        for: "newlightenddate",
        class: "eighth"})).append($('<input/>',{
            type: "text",
            id: "newlightenddate",
            name: "newlightenddate",
            class:"datepick"
        }))
  });
  $('form').on('focus',".datepick", function(){
    $(this).datepicker({
        changeMonth: true,
        changeYear: true
    });
});
 });

 $(function(){
     $("#addemphealthcomments").click(function(){
     $("#oshadataarea").find("label").remove();
     $("#oshadataarea").find("input").remove();
     $("#oshadataarea").find("textarea").remove();
     $("#oshadataarea").find("br").remove();
    $("#oshadataarea").append($('<label/>',{
        text:"Employee Comment Date",
        for: "newemplcomdate",
        class: "eighth"})).append($('<input/>',{
            type: "text",
            id: "newemplcomdate",
            name: "newemplcomdate",
            class:"datepick"
        })).append($('<br />')).append($('<label/>',{
        text:"Employee Comments",
        for: "newemplincidomments",
        class: "eighth"})).append($('<textarea/>',{
            cols: "50",
            rows: "5",
            id: "newemplincidomments",
            name: "newemplincidomments"

        }))
  });
 });

只要我在下面添加此功能,其他所有功能都会停止工作。

$function(){

    $("#incidentform").validate({
        rules: {
            incidate: "required"
        },
        messages: {
            incidate: "Please enter the incident date."
        }
    })
 }

我一定是做错了。当谈到jquery时,我几乎是一个新手,因为我只用了一个月。希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:1)

$(function(){

    $("#incidentform").validate({
        rules: {
            incidate: "required"
        },
        messages: {
            incidate: "Please enter the incident date."
        }
    })
 });

围绕它缺少括号。