2013-07-12 40 views
0
提交

我有這樣動態形式不使用jQuery

foreach($class_pool_result["data"] as $pool) { 
       $html[] = "<form action='/dashboard/attend' method='post' class='form_attend'>"; 
       $html[] = "<input type='hidden' name='pool' value='" . $Encryption->encrypt(urlencode($pool["id"])) . "' />"; 
       $html[] = "<input type='hidden' name='class' value='" . $Encryption->encrypt(urlencode($class["data"]["id"])) . "' />"; 
       $html[] = "<table>"; 
        $html[] = "<tr><td width='150'>" . $pool["plan_name"] . "</td><td><button class='attend'>Attend</button></td><td align='left'> <span style='color:#FF3902'>" . $pool["class_count"] . "</span> <small>classe(s) left</small></td></tr>"; 
        $html[] = "</table>"; 
       $html[] = "</form>"; 
      } 

然後在我的jQuery的文件我試圖讓上應該默認爲提交表單

$(document).on("click", "button.attend", function(e){ 
    alert("hey"); 
    e.preventDefault(); 
    $.post("/file.php", { form: , method: "add" }, function() { 

    }, "json"); 
}); 
按鈕單擊事件創建的窗體

它不會提醒任何事情或阻止提交。 我試過用submitbutton.attend來替換click,它的形式是類名,但是什麼也沒有。有任何想法嗎?

+1

爲什麼在文檔,而不是點擊事件的表單上的按鈕? –

+0

因爲它位於facebox彈出框內。我用標準的$(button).on(「click」)來試試它......但它仍然沒有做任何事情 – Naterade

+0

嗯,你做錯了。 –

回答

0

綁定提交到表單。

$(".form_attend").on("submit", function(){ 
//do stuff 
}); 

,改變你的按鈕:

<input type="submit" value="attend"/> 

另外,如果你動態地與未示出的一些其他的代碼添加窗體,請確保你把你將它添加到即元素的選擇:

<div id="appending-here"> 
<form></form>//your form 
</div> 

你的選擇應該是:

$("#appending-here").on("submit", ".form_attend", function(){//stuff}) 
+0

剛試過,無濟於事。我不知道發生了什麼事。 – Naterade

+0

@naterade檢查編輯 – Brad

0
$('form.myform').on('submit', function(){ 
    var that = $(this), 
     url = that.attr('action'), 
     type = that.attr('method'), 
     data = {}; 

    that.find('[name]').each(function(index, value){ 


     var that = $(this), 
      name = that.attr('name'), 
      value = that.val(); 

     data[name] = value; 

    }); 

     $.ajax({ 
      url:url, 
      type:type, 
      data:data, 
      success:function(output){ 
      $('#res').html(output).slideDown("slow"); 

      } 

     }); 

    return false; 

}); 

myForm的是你把表格標記的類的名稱.. GE