问题描述:我页面中有这样多个表单,我都是这个定义的,当我点击确定按钮时,此时能够获得相对应的表单对象,我该怎么获取到他的两个值呢?
解决方案:
页面元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <form id="form1"> <lable>姓名:</lable> <input type="text" class="user_name" /> <lable>电话:</lable> <input type="text" class="user_tel" /> <span class="button">确定</span> </form> <form id="form2"> <lable>姓名:</lable> <input type="text" class="user_name" /> <lable>电话:</lable> <input type="text" class="user_tel" /> <span class="button">确定</span> </form> <form id="form3"> <lable>姓名:</lable> <input type="text" class="user_name" /> <lable>电话:</lable> <input type="text" class="user_tel" /> <span class="button">确定</span> </form>
|
jquery 实现方法(自己任意添加一个 jquery 库即可)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $(function() { $(".button").each(function() { $(this).click(function() { var name = $(this) .parent("form") .find(".user_name") .val(); var tel = $(this) .parent("form") .find(".user_tel") .val(); console.log(name); console.log(tel); }); }); });
|