2016-01-22 130 views
1

我想合併兩個JavaScripts。第一個是使用ajax發送消息,第二個是提醒用戶有關聯繫表單中的必填字段。Ios設備,「必填」字段,並感謝您的消息

我想合併這兩個,也許與一個IF語句,所以首先檢查所有字段,然後發送消息。

1與阿賈克斯的JavaScript:

$('document').ready(function() { 
    $('form#contact-form').submit(function() { 
     var form = $(this); 
     var post_data = form.serialize(); //Serialized the form data for process.php 
     $('#loader').html('<img src="../spinner.gif" /> Please Wait...'); 

     form.fadeOut(500, function() { 
      form.html("<h3>Thank you.").fadeIn(); 
      $('#loader').html(''); 
     }); 

     // Normally would use this 
     $.ajax({ 
      type: 'POST', 
      url: 'process.php', // Your form script 
      data: post_data, 
      success: function(msg) { 
       form.fadeOut(500, function(){ 
        form.html(msg).fadeIn(); 
       }); 
      } 
     }); 

     return false; 
    }); 
}); 

2警報的JavaScript:

$('document').ready(function() { 
    $('form#contact-form').submit(function(e) { 

     var ref = $(this).find("[required]"); 

     $(ref).each(function(){ 
      if ($(this).val() == '') 
      { 
       alert("Required field should not be blank."); 

       $(this).focus(); 

       e.preventDefault(); 
       return false; 
      } 
     }); return true; 
    }); 
}); 

從答案下面我創建了下面的代碼。

我做了這個鏈接,如果有人想幫助。警報正常工作,但代碼不會停止。它繼續加載其餘代碼。

https://jsfiddle.net/L8huq1t1/

+0

你爲什麼要合併的代碼?認真考慮單一責任。 –

+0

你使用jquery.validate文件還是你想自定義驗證? – KinjalMistry

+0

這是一個聯繫表單。我的問題是,在Ios中,所需的字段不起作用。有了這個JavaScript,我設法提醒用戶。 – Yvette

回答

1

您可以通過下面的代碼做到這一點。

function checkValidation() { 
     var ref = $(this).find("[required]"); 
     $(ref).each(function(){ 
      if ($(this).val() == '') 
      { 
       alert("Required field should not be blank."); 
       $(this).focus(); 
       //e.preventDefault(); 
       return false; 
      } 
     }); 
     return true; 
    } 
$('document').ready(function() { 
    $('form#contact-form').submit(function() { 
     if(!checkValidation()) { 
      return false; 
     } 

     var form = $(this); 
     var post_data = form.serialize(); //Serialized the form data for process.php 
     $('#loader').html('<img src="../spinner.gif" /> Please Wait...'); 

     form.fadeOut(500, function() { 
      form.html("<h3>Thank you.").fadeIn(); 
      $('#loader').html(''); 
     }); 

     // Normally would use this 
     $.ajax({ 
      type: 'POST', 
      url: 'process.php', // Your form script 
      data: post_data, 
      success: function(msg) { 
       form.fadeOut(500, function(){ 
        form.html(msg).fadeIn(); 
       }); 
      } 
     }); 
     return false; 
    }); 
}); 

但我給你一個建議,使用jquery.validate插件這是更好的選擇。但是如果你仍然想這樣做,那麼這也是可以的。

+0

謝謝你的回答。我測試了你的代碼,但在ios不工作!我發現一個小錯誤 //e.preventDefault(); 並更正它 e.preventDefault();我會再試一次看。 – Yvette

+0

你的代碼比較容易理解,但我找不到錯在哪裏。在Ios設備上,似乎該功能不起作用。 – Yvette

+0

我發佈了我現在的代碼。 – Yvette

0

你可以使用jQuery驗證插件,它有一個表單提交處理器,你可以把你的AJAX。 Link to the plugin

您的代碼應該是這個樣子:

$('#contact-form').validate({ 
 
    rules: { 
 
    your_input_name: { 
 
     required: true 
 
    } 
 
    }, 
 
    messages: { 
 
    your_input_name: { 
 
     required: 'Field is required' 
 
    } 
 
    }, 
 
    submitHandler: function() { 
 
    var form = $(this); 
 
    var post_data = form.serialize(); //Serialized the form data for process.php 
 
    $('#loader').html('<img src="../spinner.gif" /> Please Wait...'); 
 

 
    form.fadeOut(500, function() { 
 
     form.html("<h3>Thank you.").fadeIn(); 
 
     $('#loader').html(''); 
 
    }); 
 

 
    // Normally would use this 
 
    $.ajax({ 
 
     type: 'POST', 
 
     url: 'process.php', // Your form script 
 
     data: post_data, 
 
     success: function(msg) { 
 
     form.fadeOut(500, function() { 
 
      form.html(msg).fadeIn(); 
 
     }); 
 
     } 
 
    }); 
 

 
    return false; 
 
    } 
 
});

+0

謝謝你的回答。我測試了它,它不工作... – Yvette