0

我有这样创建的表格

    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 文件中,我尝试在按钮上获取 click 事件,该按钮应该默认提交表单

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

    }, "json");
});

它不会提醒任何东西或阻止提交。我尝试用表单类名替换和替换,但click没有submitbutton.attend有任何想法吗?

4

2 回答 2

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})
于 2013-07-12T16:30:17.813 回答
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 is the name of the class you put in the form tag.. ge

于 2013-07-12T16:39:46.770 回答