javascript ID selector

javascript ID selector

本文关键字:selector ID javascript      更新时间:2023-09-26

我有两种形式的

<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,以便在form2

中单击时将max2大res
 var max_res = $('input[id^="max"]', $(this).parent()).val();
 //selects the id starting with max in this form
 var  Results = "Max Result " + max_res; 
var i = 1;
var max_res = $("input#max"+i).val();

/编辑:我会把公共类添加到所有输入中,比如class="max",然后:

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