JQuery的 - 它的形式提交? [英] JQuery - which form was submitted?

查看:97
本文介绍了JQuery的 - 它的形式提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有产品页面并为他们每个人,我希望有一个使用AJAX来更新会话的形式。我已经做到了这一点位 - 只需提供背景

我用的是产品的ID来创建一个不同的窗体类,并将其命名为每个表单,所以每个形式被称为像 this_form_name_567 ,下一个会是 this_form_name_568 等等。

有一个每个表单提交按钮。我无法搞清楚

  1. 哪个事件最好使用使被点击提交按钮时,正确的形式将被识别?

  2. 单击后,如何再确保正确的值是从一个隐藏字段(唯一ID)提交的表单中,这样我可以填充线code,如:

      $后(compare_response.php,{compare_this:$(#compare_this)VAL()},功能(数据){
    }
     

解决方案

您可以使用 .closest 树遍历方法获取的形式,其中感兴趣的按钮是嵌套的:

  $(输入[类型=提交])。点击(函数(){
    警报($(本).closest(形式)ATTR(ID));
});
 

或更简单,只得到元素的表格属性:)

  $(输入[类型=提交])。点击(函数(){
    警报(this.form.id);
});
 

您可以尝试一下这里。

I have a page of products and for each of them, I want to have a form that uses AJAX to update the session. I've done this bit - just providing background.

I use the product's ID to create a different form class and name for each form, so each form is called something like this_form_name_567 and the next would be this_form_name_568 and so on.

There is a submit button per form. I'm having trouble figuring out

  1. Which event is best to use so that the correct form will be identified when a submit button is clicked?

  2. Once clicked, how to then make sure the correct value is taken from a hidden field (unique ID) within the submitted form so that I can populate a line of code such as:

    $.post("compare_response.php", {compare_this: $("#compare_this").val()}, function(data){
    }
    

解决方案

You can use the .closest tree traversal method to get the form in which the button of interest is nested:

$("input[type=submit]").click(function() {
    alert($(this).closest("form").attr("id"));
});

or even simpler, just get the element's form property :)

$("input[type=submit]").click(function() {
    alert(this.form.id);
});

You can try it out here.

这篇关于JQuery的 - 它的形式提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆