javascript - Getting the id of a form on onsubmit Event - Stack Overflow

Hello i am trying to get the id of a form of which the onsubmit event is triggered. I have lots of form

Hello i am trying to get the id of a form of which the onsubmit event is triggered. I have lots of forms in a page so i need to do this. I have tried the ones in the test method and all of those return "undefined"

<form id="addToBasketForm_39"  title="" method="post" onsubmit="test()">
          <input type="hidden" name="product_id" value="39">
          <input type="hidden" name="_token" value="M1kqlhvmIsZr9N8amTF84lwZeTuMgopHchEt4nhs">
            <a id="submit" class="add-to-cart" onclick="$('#addToBasketForm_39').submit()" >
            <i class="fa fa-shopping-cart"></i>Sepete Ekle</a>
        </form> 



function test(){
 var formID = $(this).parents("form").attr("id");

//var formID = $(this).closest("form").attr('id');
//var formID = $(this).closest("form[id]").attr('id');
alert(formID + ' form submitted');

}  

Hello i am trying to get the id of a form of which the onsubmit event is triggered. I have lots of forms in a page so i need to do this. I have tried the ones in the test method and all of those return "undefined"

<form id="addToBasketForm_39"  title="" method="post" onsubmit="test()">
          <input type="hidden" name="product_id" value="39">
          <input type="hidden" name="_token" value="M1kqlhvmIsZr9N8amTF84lwZeTuMgopHchEt4nhs">
            <a id="submit" class="add-to-cart" onclick="$('#addToBasketForm_39').submit()" >
            <i class="fa fa-shopping-cart"></i>Sepete Ekle</a>
        </form> 



function test(){
 var formID = $(this).parents("form").attr("id");

//var formID = $(this).closest("form").attr('id');
//var formID = $(this).closest("form[id]").attr('id');
alert(formID + ' form submitted');

}  
Share Improve this question asked Jun 13, 2017 at 12:05 rematnarabrematnarab 1,3255 gold badges25 silver badges46 bronze badges 1
  • `function test(event) { var formId = event.target.id; }? – putvande Commented Jun 13, 2017 at 12:07
Add a ment  | 

3 Answers 3

Reset to default 4

Here is how to assign to all submit links unobtrusively

Also NEVER use the name or ID submit it can easily hide the submit event

Lastly IDs need to be unique, I removed the no longer used ID from the anchor

$(function() {
  $(".add-to-cart").on("click", function(e) { // anything with class add-to-cart
    e.preventDefault(); // cancel the link
    var $form = $(this).closest("form"); // The form this add-to-cart is in
    test($form.attr("id")); // not sure what you want here
    $form[0].submit(); // submit the actual DOM form
  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form id="addToBasketForm_39" title="" method="post">
  <input type="hidden" name="product_id" value="39">
  <input type="hidden" name="_token" value="M1kqlhvmIsZr9N8amTF84lwZeTuMgopHchEt4nhs">
  <a class="add-to-cart" href="#"><i class="fa fa-shopping-cart"></i>Sepete Ekle</a>
</form>

I would remend you to use unobtrusive event handler instead of ugly inline event handler.

$('form').on('submit', test)

In the function

function test(){
  console.log(this.id);
}

As per the current code, pass the current element context i.e. this

<form id="addToBasketForm_39" onsubmit="test(this)">

Access the context in the code

function test(element){
  console.log(element.id)
}
<form id="addToBasketForm_39"  title="" method="post" onsubmit="test(this)">
          <input type="hidden" name="product_id" value="39">
          <input type="hidden" name="_token" value="M1kqlhvmIsZr9N8amTF84lwZeTuMgopHchEt4nhs">
            <a id="submit" class="add-to-cart" onclick="$('#addToBasketForm_39').submit()" >
            <i class="fa fa-shopping-cart"></i>Sepete Ekle</a>
        </form> 

<script type="text/javascript"> 
    function test(event)
    {
        var id = event.id;
        alert(id);
    }
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744772723a4592857.html

相关推荐

  • javascript - Getting the id of a form on onsubmit Event - Stack Overflow

    Hello i am trying to get the id of a form of which the onsubmit event is triggered. I have lots of form

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信