javascript - 仅选择javascript中的输入字段

标签 javascript

我的 html 代码中有很多下拉菜单、复选框和输入类型文本字段。所有这些字段共享相同的 ID,该 ID 以: order-billing_address....{field_name} 我只想获得输入类型的文本字段。

我尝试使用 querySelectorAll 方法:

var billingFields = document.querySelectorAll('*input[id^="order-billing_address"]');

但我遇到语法错误:SyntaxError: '*input[id^="order-billing_address"]' is not a valid selector。我怎样才能让我的选择器工作?

最佳答案

你可以传递type

var billingFields = document.querySelectorAll('input[type="text"][id^="order-billing_address"]');
console.log(billingFields)
<input type='checkbox' id='order-billing_address_1'>
<input type='checkbox' id='order-billing_address_2'>
<input type='radio' id='order-billing_address_3'>
<input type='radio' id='order-billing_address_4'>
<input type='text' id='order-billing_address_5'>
<input type='text' id='order-billing_address_6'>

关于javascript - 仅选择javascript中的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55065247/

相关文章:

javascript - dabeng 组织结构图 Php Mysql Ajax

javascript - 页面加载后如何在 jquery 中调用 onclick 事件

javascript - 使用 localStorage 存储和检索 PNG 时的空白图像

javascript - 如何在mvc 5中基于静态下拉列表过滤数据库记录

javascript - 对无限 ajax 滚动如何检索其他帖子感到困惑

javascript - 字符串数组 toUpperCase() 不起作用

javascript - Bloodhound typeahead 在控制台 laravel 中未定义

javascript - 隐藏固定页脚?

javascript - 如何使用 Babel 作为 CLI 程序?

javascript - 相对于背景大小为 : cover 的特定图像点的元素