JavaScript ID 选择器

标签 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从ma​​x1更改为ma​​x2,以便我可以将ma​​x2的值存储在中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/

相关文章:

javascript - 我可以在 Node 服务器停止后调用 $modal.open

javascript - 从插件获取本地文件到javascript

c# - 多个字段的 MVC 表单验证

php - 序列化/反序列化 php 对象

javascript - Angular 2 中的 ng 重复

javascript - 使用ajax发送数据表单文件

javascript - 如何在 for 循环中将数据插入到新对象中?

jquery - 从 jwplayer 6 中删除 "share - embed - info"选项卡框

jquery - 目标类的单个实例

php - 在 &lt;textarea&gt; 中格式化换行符