获取值div内部隐藏的输入

时间:2018-11-01 17:23:55

标签: javascript jquery

我需要获取隐藏输入的值,(。red是Div_Gral的div子代) 这是我所做的:

$('#div_Gral').each(function () {

  var tiem = "";
  tiem = $(this).find(".red > hidden").val();

  if (tiem = "") {

  } else { 
    alert(tiem); 
  }

});

1 个答案:

答案 0 :(得分:0)

您不能在div上使用.each,因为它只会遍历列表,而div不是。您首先需要使用red类获取所有元素,然后对它们进行迭代。另外,为了知道是否隐藏了某些内容,可以使用.is(":hidden"),它将返回true或false。以下代码应该给您一个想法。

// Fetch all inputs that have the clas "red"
var inputs = $('#div_Gral').find('.red');

// Now iterate through them and check if they are hidden.
$(inputs).each(function() {

  // If so, show an alert with it's value.
  if($(this).is(":hidden")) {
    alert($(this).val());
  }
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div_Gral">
  <input class="red" value="I am not hidden.">
  <input class="red" hidden="hidden" value="I am hidden.">
</div>

相关问题