2012-11-15 106 views
0

是否有可能使用jQuery獲取表單中的動態值?動態加載表單元素值

例如,我有這樣的:

<form id="contact" method="post" action"contact.php" class="ajaxForm"> 
    <label>Name:</label> 
    <input type="text" name="name" /> 
    <label>Email:</label>  
    <input type="text" name="email"/> 
    <label>Message:</label> 
    <textarea name="message"></textarea> 
    <input type="submit" value="send!"/> 
</form> 

在JS:

$(".ajaxForm").send(function(){ 
    var page = $(this).attr("action"); 
}); 

我想變數名稱=一些環一樣。每次但代碼值僅讀取輸入/ textarea的/選擇數據。

感謝您的幫助!

回答

3

您可以用jQuery的形式獲得輸入/ textarea的/選擇值。

$(".ajaxForm").send(function(){ 
    var page = $(this).attr("action"); 
    var its = $(this).find('input, textarea, select'); 
    its.each(function(){ 
     console.log($(this).val()); 
    }) 
}); 
2

使用.children()

$(".ajaxForm").send(function(){ 
    var page = $(this).attr("action"); 
    $(this).children('input').each(function() { // Loop over all the children 
               // that are input elements 
      console.log($(this).val()); 
         OR 

      console.log(this.value); 
    }); 

});