我的 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/