用于获取每个输入值

时间:2015-12-30 08:03:28

标签: javascript jquery

我期待值1和2,但它返回null。知道为什么会这样吗?

$(function() {

  $('.agent_rows').each(function() {
    console.log($(this).find('input[name="associatedAgentId"]').val())
  })

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="agent_rows">
  <input type="hidden" name="associatedAgentId" value="1">
</div>

<div class="agent_rows">
  <input type="hidden" name="associatedAgentId" value="2">
</div>

2 个答案:

答案 0 :(得分:1)

您设置的属性不正确。它应该是value而不是属性中的val

 <div class="agent_rows">
   <input type="hidden" name="associatedAgentId" value="1">
 </div>

 <div class="agent_rows">
   <input type="hidden" name="associatedAgentId" value="2">
 </div>

答案 1 :(得分:0)

应该是value而不是val

<div class="agent_rows">
  <input type="hidden" name="associatedAgentId" value="1">
</div>

<div class="agent_rows">
  <input type="hidden" name="associatedAgentId" value="2">
</div>

工作代码段

$(function() {
  $('.agent_rows').each(function() {
    console.log($(this).find('input[name="associatedAgentId"]').val())
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="agent_rows">
  <input type="hidden" name="associatedAgentId" value="1">
</div>

<div class="agent_rows">
  <input type="hidden" name="associatedAgentId" value="2">
</div>