javascript - 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>
解决方案
也许试试这个:
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);
}
}
推荐阅读
- javascript - ClearInterval 未定义 - jQuery
- visual-studio-code - 如何使 vscode 不记住/记录最近打开的文件或文件夹?
- sql - 间隔日期到天
- python - 为什么在单击按钮之前执行该功能?
- webpack - 应用程序内的 Framework7 生产或开发环境
- project-reactor - 项目反应堆背压(缓冲区大小?)问题
- symfony - Composer/Symfony:依赖问题
- python - 查找具有指定数值列的最高平均值(平均值)的分类列
- react-admin - React-Admin 链接两个字段
- python - 使用 subprocess.Popen 时,mongoexport 的输出在 stderr