最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery multiple forms in same page - Stack Overflow

programmeradmin9浏览0评论

I have the following dynamically generated 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. I am using Jquery to perform some validation when the form is submitted. 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? Thanks

I have the following dynamically generated 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. I am using Jquery to perform some validation when the form is submitted. 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? Thanks

Share Improve this question asked Jul 25, 2012 at 17:58 user1625066user1625066
Add a ment  | 

2 Answers 2

Reset to default 7

Add some context:

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

Use this (the form-element) as context-argument:

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

评论列表(0)

  1. 暂无评论