首页 > 解决方案 > javascript jquery找到多个没有特定属性的隐藏输入

问题描述

javascript 或 jquery 是否可以使用随机创建的 id 从多个隐藏输入创建一个值数组(换句话说,没有要搜索的特定属性)?下面的代码只会导致第一个隐藏输入“abc”的警报......
谢谢

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

<script>
//create hidden fields array
var hiddenFields = [];

//for each table row
$('html').each(function()
{
  //get hidden field 
  if (hiddenField != $(this).find("input[type='hidden']").val()){
  var hiddenField = $(this).find("input[type='hidden']").val();
  }
  //if not empty push to array
  if(hiddenField!='undefined'&& hiddenField !=null )
    hiddenFields.push(hiddenField);
});
alert(hiddenFields);
</script>

标签: javascriptjquery

解决方案


也许试试这个:

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" id="some_random_id" value="abc" />
<input type="hidden" id="some_random_id" value="def" />
<input type="hidden" id="some_random_id" value="ghi" />

JS

var tags = document.getElementsByTagName("input");

for(var i = 0; i < tags.length; i++){
  if(tags[i].getAttribute("hidden") == null){
    console.log(tags[i].value);
  }
}

Codepen - https://codepen.io/anon/pen/jxRVMb?editors=1010


推荐阅读