我在具有不同ID的页面中设置了多种形式。我想获取表单的ID,当其中之一由Submit btn提交时。这是我到目前为止所得到的

<div class="form-wrap">
<form action="../administrator/apps/image_process_2D.php" method="post" enctype="multipart/form-data" id="upload_form_2D" class="submit_form">
    <input name="__files[]" type="file" multiple />
    <input name="__submit__" type="submit" class="btn btn-info submit_form" value="Upload"/>
</form>

<div class="form-wrap">
<form action="../administrator/apps/image_process_3D.php" method="post" enctype="multipart/form-data" id="upload_form_3D" class="submit_form">
    <input name="__files[]" type="file" multiple />
    <input name="__submit__" type="submit" class="btn btn-info submit_form" value="Upload"/>
</form>

<script>
    var my_form_id = '#upload_form'; //ID of an element for response output
    $(my_form_id).on( "submit", function(event) {
    var post_url = $(this).attr("action");
    //codes here//
    });
</script>


因此,如果单击“提交”按钮时的表单ID,则需要'my_form_id'取值。

任何帮助,如果不胜感激

最佳答案

您可以使用closest()遍历到最接近的父表单元素,并使用propattr获取元素的ID:

$('form').on( "submit", function(event) {
     console.log($(this).closest('form').prop('id'));
});

10-01 01:09
查看更多