我有 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的值存储在中max-res 当在 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;
关于JavaScript ID 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6960116/