javascript ID选择器

时间:2011-08-05 17:16:42

标签: javascript jquery forms css-selectors

我有2个表格

<form name= "form1" action="">
  <input type="hidden" name="max" id= "max1" value="100"/> 
  <input type="submit"  class="submit" value="part 1" />
</form>

<form name= "form2" action="">
  <input type="hidden" name="max2" id= "max2" value="200"/> 
  <input type="submit"  class="submit" value="part 2" />
</form>

我从这个表单中获取值

         $(".submit").click(function () {

 here  -->       var max_res = $("input#max1").val();
                 var  Results = "Max Result " + max_res;      
         });

我的问题是如何动态地将ID从 max1 更改为 max2 ,以便我可以在中存储 max2 的值在form2中进行单击时的max-res

3 个答案:

答案 0 :(得分:1)

var i = 1;
var max_res = $("input#max"+i).val();

/ edit:我会在所有输入中添加公共类,例如class =“max”,然后:

var max_res = $(this).parent().find("input.max").val();

答案 1 :(得分:1)

 var max_res = $('input[id^="max"]', $(this).parent()).val();
 //selects the id starting with max in this form
 var  Results = "Max Result " + max_res; 

答案 2 :(得分:0)

var max_res = $(this).closest("form").find("input:hidden").val()