简体   繁体   English

jQuery在同一页面中的多种形式

[英]Jquery multiple forms in same page

I have the following dynamically generated HTML 我有以下动态生成的HTML

<div id="1">
<form name = "inpForm">
<input name="FirstName" type="text"/>
<input type="submit" value="Submit"/>
</form>
</div>

<div id="2">
<form name = "inpForm">
<input name="FirstName" type="text"/>
<input type="submit" value="Submit"/>
</form>
</div>

The outer divs have different IDs but the form names are the same. 外部div具有不同的ID,但表单名称相同。 I am using Jquery to perform some validation when the form is submitted. 提交表单时,我正在使用Jquery执行一些验证。 However, when the second form is submitted, I always get the values of the first form. 但是,提交第二个表单时,我总是会得到第一个表单的值。

$(document).ready(function () {
    $('form[name="inpForm"]').live('submit', function () {
        alert($('input[name="FirstName"]').val());
        return false;
    });
});

How can I modify myJquery to find the "FirstName" element that matches the current form where the submit was triggered? 如何修改myJquery以查找与触发提交的当前表单相匹配的“名字”元素? Thanks 谢谢

添加一些上下文:

alert($(this).find('input[name="FirstName"]').val());

使用this (表单元素)作为上下文参数:

alert($('input[name="FirstName"]',this).val());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM