用于Ajx调用后无法刷新页面的唯一值检查的Js验证程序插件远程规则

时间:2019-06-24 04:37:41

标签: javascript jquery validation jquery-plugins

我正在尝试使用远程规则来验证名称。 在首次提交表单时,请输入test值,并使用ajax提交表单和绑定值。但是第二次没有刷新页面后,我试图通过添加与我添加的相同值test来提交表单。我不会因为现有名称而出错。

这是js代码

  $("#designationaddedit").validate({
     rules: {
         designation_name: {
             required: true,
         },
         designation_copy_name: {
             remote: {
                 url: base_url + 'Designation/designation_name_exists',
                 type: "post",
                 data: {
                    designation_copy_name: function() {
                        console.log("1");
                      return $( "#designation_copy_name" ).val();
                    }
                }
             }
         },
     },
     messages: {
         designation_name: {
             required: "Enter Designation name"
         },
         designation_copy_name: {
             remote: 'Designation name is already exists.'
         },
     },
 });


 $(document).on('submit', '#designationaddedit', function(event) {
         event.preventDefault();


         var designation_id = $('#designation_id').val();
         var designation_name = $('#designation_name').val();
         var action = $('#action').val();
         var table = $('#designationTable').DataTable();
         var info = table.page.info();
         var currentpage = info.start;

         if ($("#designationaddedit").valid()) {
             $('.preloader').show();
             $.ajax({
                 url: base_url + 'designation/add',
                 type: 'POST',
                 dataType: 'json',
                 data: { submit: 1, designation_id: designation_id, designation_name: designation_name, action: action },
                 success: function(response) {
                     $('.preloader').hide();
                      if (response.success == 1) {
                     $("#designation_model").modal('hide');

                     } else {

                     }
                 }
             });
         }
     })

1 个答案:

答案 0 :(得分:1)

下面的代码解决了我的问题。希望对您有帮助。

remote: { url: "http:url.com", type: "post", data: { USER_ID: userid }, async: false, // set async = false }