Javascript jQuery-如何从同一类中隐藏的输入id中获取不同的值?

Javascript jQuery-如何从同一类中隐藏的输入id中获取不同的值?,javascript,jquery,Javascript,Jquery,我想从同一个类中隐藏的输入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" valu

我想从同一个类中隐藏的输入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>
不要使用“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
});

不要将“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
});

不要将“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
});

不要将“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
});

最好为p和输入标记设置类,而不是使用id

 <li>
   <div class="test"><img src="course.png"/><p class="text">Computer</p>
    <span class="list">
      <p class="myCourse">Course 1 <input type="hidden" class="myCourseVal" value="1"></p>
      <p class="myCourse">Course 2 <input type="hidden" class="myCourseVal" value="2"></p>
      <p class="myCourse">Course 3 <input type="hidden" class="myCourseVal" value="3"></p>
    </span> 
   </div>
  </li>

$(".test .myCourse").click(function(){
    var myValue = $(this).find('.myCourseVal').val()); 
    alert(myValue);
});

最好为p和input标记使用类,而不是id使用它们

 <li>
   <div class="test"><img src="course.png"/><p class="text">Computer</p>
    <span class="list">
      <p class="myCourse">Course 1 <input type="hidden" class="myCourseVal" value="1"></p>
      <p class="myCourse">Course 2 <input type="hidden" class="myCourseVal" value="2"></p>
      <p class="myCourse">Course 3 <input type="hidden" class="myCourseVal" value="3"></p>
    </span> 
   </div>
  </li>

$(".test .myCourse").click(function(){
    var myValue = $(this).find('.myCourseVal').val()); 
    alert(myValue);
});

最好为p和input标记使用类,而不是id使用它们

 <li>
   <div class="test"><img src="course.png"/><p class="text">Computer</p>
    <span class="list">
      <p class="myCourse">Course 1 <input type="hidden" class="myCourseVal" value="1"></p>
      <p class="myCourse">Course 2 <input type="hidden" class="myCourseVal" value="2"></p>
      <p class="myCourse">Course 3 <input type="hidden" class="myCourseVal" value="3"></p>
    </span> 
   </div>
  </li>

$(".test .myCourse").click(function(){
    var myValue = $(this).find('.myCourseVal').val()); 
    alert(myValue);
});

最好为p和input标记使用类,而不是id使用它们

 <li>
   <div class="test"><img src="course.png"/><p class="text">Computer</p>
    <span class="list">
      <p class="myCourse">Course 1 <input type="hidden" class="myCourseVal" value="1"></p>
      <p class="myCourse">Course 2 <input type="hidden" class="myCourseVal" value="2"></p>
      <p class="myCourse">Course 3 <input type="hidden" class="myCourseVal" value="3"></p>
    </span> 
   </div>
  </li>

$(".test .myCourse").click(function(){
    var myValue = $(this).find('.myCourseVal').val()); 
    alert(myValue);
});

那么问题是什么?下面的成员解决了:DSo问题是什么?下面的成员解决了:DSo问题是什么?下面的成员解决了:DSo问题是什么?下面的成员解决了:Dtanks hadi,你的代码激励我变得更好,我也注意到了你的建议和方法:汉克斯·哈迪,你的代码激励我变得更好,我也注意到了你的建议和方法:汉克斯·哈迪,你的代码激励我变得更好,我也注意到了你的建议和方法:汉克斯·哈迪,你的代码激励我变得更好,我也注意到了你的建议和方法:D