2014-10-13 27 views
-1

我正在編寫用於驗證和提交表單的Jquery代碼。我有錨定標記包含值。該功能是....我想獲取錨點數據併發布它....簡單...如何做到這一點?在發送方法之前添加使用Ajax發送錨點數據

(function ($) { 

      "use strict"; 

      $(document).ready(function() { 

      if ($('#hcm_new_applicant_form').length) { 
         $("#hcm_new_applicant_form").validate({ 
          // Rules for form validation 
          rules: {      
           applicant_name: { 
            required: true 
           }, 
           cnic: { 
            required: true 
           }, 
           mobilephonereq: { 
            required: true 
           },           
           gender: { 
            required: true 
           }      
          }, 

          // Messages for form validation 
          messages: { 
           applicant_name: { 
            required: 'Please enter applicant name' 
           },      
           cnic: { 
            required: 'Please provide CNIC number' 
           }, 
           mobilephonereq: { 
            required: 'Please enter mobile phone' 
           }, 
           gender: { 
            required: 'Please select gender' 
           }      
          }, 


          // Ajax form submition 
          /* Previous CODE      
          submitHandler: function (form) { 
           $(form).ajaxSubmit({ 
            beforeSend: function() {        
             var anchors = document.getElementsByName('hcm_position_tags'); 
             var myAnchor = anchors[0]; 
             var value = myAnchor .getAttribute('value'); // This give inside value 
             var html = myAnchor .innerHTML; // This give selected tags               
            }, 

            success: function() {                       
             $('<input />').attr('type', 'hidden') 
              .attr('name', "hcm_all_position_tags") 
              .attr('value', html) 
              .appendTo('#hcm_new_applicant_form'); 
             return true; 
            } 
           }); 
          }, 

          Previous CODE 
    // Ajax form submition            
        submitHandler: function (form) { 
         var $tag = $('hcm_position_tags'); 
         $(form).ajaxSubmit({ 
          data: { 
           tags: $tag.attr('value'), 
           html: $tag.html() 
          }, 
          success: function() { 
           $('<input />').attr('type', 'hidden') 
            .attr('name', "hcm_all_position_tags") 
            .attr('value', html) 
            .appendTo('#hcm_new_applicant_form'); 
           return true; 
          } 
         }); 
         return false; 
        }, */ 
/* NEW CODE */ 
// Ajax form submition            
       submitHandler: function (form) { 
        var $tag = $('#tags_hcm_position'); 

        var html = $tag.html(); // This give selected tags 
        alert($tag.length + ':' + html + ':' + $tag.attr('value')); 

        $('<input />', { 
         type: 'hidden', 
         'name': "hcm_all_position_tags", 
         'value': html 
        }).attr('type', 'hidden').appendTo(form); 

        form.submit(); 
       },   

          errorPlacement: function (error, element) { 
           error.insertAfter(element.parent()); 
          } 
         }); 
        } 

     }} 

以前我是用這個

$("#hcm_new_applicant_form").submit(function(eventObj) { 
    var anchors = document.getElementsByName('hcm_position_tags'); 
    var myAnchor = anchors[0]; 
    var value = myAnchor .getAttribute('value'); // This give inside value 
    var html = myAnchor .innerHTML; // This give selected tags 

    $('<input />').attr('type', 'hidden') 
     .attr('name', "hcm_all_position_tags") 
     .attr('value', html) 
     .appendTo('#hcm_new_applicant_form'); 
    return true; 
}); 

的問題是,我得到的POST數據,但驗證不工作。我想有驗證+崗位的附加數據...

沒有得到......怎麼辦在HTML

hcm_position_tag是

<a href="#" id="tags_hcm_position" name="hcm_position_tags" data-name="tags_hcm_position" data-type="select2" data-pk="1" data-title="Enter position applied for" class="editable editable-click" style="display: inline; background-color: rgba(0, 0, 0, 0);">Teaching</a> 

收到錯誤 「0:未定義:未定義」

+0

告訴我們您的HTML代碼。 – Ravimallya

回答

0

你需要的是data,不beforeSend

(function ($) { 
    "use strict"; 
    $(document).ready(function() { 
     if ($('#hcm_new_applicant_form').length) { 
      $("#hcm_new_applicant_form").validate({ 
       // Rules for form validation 
       rules: { 
        applicant_name: { 
         required: true 
        }, 
        cnic: { 
         required: true 
        }, 
        mobilephonereq: { 
         required: true 
        }, 
        gender: { 
         required: true 
        } 
       }, 

       // Messages for form validation 
       messages: { 
        applicant_name: { 
         required: 'Please enter applicant name' 
        }, 
        cnic: { 
         required: 'Please provide CNIC number' 
        }, 
        mobilephonereq: { 
         required: 'Please enter mobile phone' 
        }, 
        gender: { 
         required: 'Please select gender' 
        } 
       }, 


       // Ajax form submition            
       submitHandler: function (form) { 
        var $tag = $('hcm_position_tags'); 

        var html = $tag.html(); // This give selected tags 
        alert($tag.length + ':' + html + ':' + $tag.attr('value')) 

        $('<input />', { 
         type: 'hidden', 
         'name': "hcm_all_position_tags", 
         'value': html 
        }).attr('type', 'hidden').appendTo(form); 

        form.submit(); 
       }, 

       errorPlacement: function (error, element) { 
        error.insertAfter(element.parent()); 
       } 
      }); 
     } 

    }) 
}) 

這將向服務器發送2個附加參數,稱爲tagshtml(將它們更改爲任何您想要的值)。

+0

不能正常工作:(表單沒有發帖 –

+0

@AtifNaseem在控制檯中有任何錯誤?頁面在提交後是否刷新? –

+0

我刪除了行submitHandler:function(form){}並放置了代碼。 –