我想从同一类中的隐藏输入 id 获取不同的值。以下是我尝试创建的一些 html 和 jQuery 示例代码。
<li>
<div class="test"><img src="course.png"/><p class="text">Computer</p>
<span class="list">
<p>Course 1 <input type="hidden" id="id1" value="1"></p>
<p>Course 2 <input type="hidden" id="id2" value="2"></p>
<p>Course 3 <input type="hidden" id="id3" value="3"></p>
</span>
</div>
</li>
我正在尝试创建的代码
$(".test p").click(function(){
var id= $(this).find('#id1').val();
var id= $(this).find('#id2').val();
var id= $(this).find('#id3').val();
alert(id); // will return value 1 or 2 or 3 if click on any course
});
最佳答案
不要使用“id”作为变量名称。
这应该有效:
$(".test p").click(function(){
var myID = $(this).find('input').val();
alert(myID); // will return value 1 or 2 or 3 if click on any course
});
关于javascript - jQuery - 如何从同一类中的隐藏输入 id 获取不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28749258/