开发者

javascript ID selector

开发者 https://www.devze.com 2023-03-26 09:41 出处:网络
I have 2 forms <form name= \"form1\" action=\"\"> <input type=\"hidden\" name=\"max\" id= \"max1\" value=\"100\"/>

I have 2 forms

<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开发者_C百科="max2" id= "max2" value="200"/> 
  <input type="submit"  class="submit" value="part 2" />
</form>

I get the values from this form here

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

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

My question is how can i dynamically change the id from max1 to max2 so I can store max2's value in max-res when a click is made in form2


 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();

/edit: I would add common class to all inputs like class="max" and then:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消