JavaScript的jQuery的发现没有特定属性的多个隐藏输入

问题描述 投票:0回答:4

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>
javascript jquery
4个回答
1
投票

你只是叫.val一旦你.find后,所以它只返回了jQuery集合中第一个元素的值。 ($('html').each只会重复一次,因为只有一个html标签的文档中)

你可以尝试这样的事情,而不是,没有jQuery的需要:

const hiddenFields = [...document.querySelectorAll('input[type="hidden"]')]
  .map(input => input.value);
  
console.log(hiddenFields);
<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" />

你也应该尝试,以便有没有重复的ID来修复HTML;这是无效的。

如果你想使用jQuery迭代:

const hiddenFields = $.map($('input[type="hidden"]'), input => $(input).val());
console.log(hiddenFields);
<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" />

2
投票

也许试试这个:

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


0
投票

可以使用filter()map()的组合

var results = $("input[type='hidden']").filter(function() {
  return this.value // only return elements that have value
}).map(function() {
  return this.value // pass the value to array
}).get()

console.log(results)
<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" />

0
投票

Grab all hidden inputs and then you can fetch value by iterating on it using forEach loop

const hiddenInputs = document.querySelectorAll('input[type="hidden"]');

const hiddenInputValues = [];

hiddenInputs.forEach((ele) => {
    hiddenInputValues.push(ele.value);
});

console.log(hiddenInputValues);
<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" />
© www.soinside.com 2019 - 2024. All rights reserved.