-2

我在一个 Div 中有一个表单,它本身在一个循环中。我想根据单击的 Div 提交提交值中包含的变量。但无论我点击什么 Div 创建,我总是得到相同的结果。我已经尝试了许多其他方法 $(this).next('form') 但没有任何效果。

HTML:

while (condition) {
    <div class="mail_summary">
        <form class="form_send_id" method="post" action="">
            <input type="hidden" name="form_send_id" value="$variable">
        </form>
    </div>
}

脚本:

<script>
    $(".mail_summary").click(function(){ 
        $('.form_send_id').submit();
    });  
</script>

在此先感谢您的帮助。

4

1 回答 1

0

我假设while循环不是 JavaScript,而是您用于创建 HTML 的语言,并且此while循环创建的不止一个div包含自己的表单。

有了这个假设,您可以简单地在闭包中引用事件的目标以正确提交表单,然后对子表单进行一些 DOM 遍历,如下所示:

$(".mail_summary").click(function(event){ 
    $(event.target).find('.form_send_id').submit();
}); 

如果出于某种原因,您正在使用 JavaScript 渲染您的 div(我不确定如何,但我希望不会),那么您的表单将以需要事件委托的方式创建。为此,您需要使用 jQuery 的on方法给事件一个委托,如下所示:

$("body").on("click", ".mail_summary", function(event){ 
    $(event.target).find('.form_send_id').submit();
}); 

我希望这会有所帮助

于 2013-08-13T05:56:54.630 回答