[英]How to access the value of an input field by its value and name attribute using Javascript
I'd like to select an input: <input id="tb-radio-gym_custom_radio-10ce67e3" type="radio" value="OUI" name="form_fields[gym_custom_radio]" data-price-inc="0" checked="checked">
我想 select 输入:
<input id="tb-radio-gym_custom_radio-10ce67e3" type="radio" value="OUI" name="form_fields[gym_custom_radio]" data-price-inc="0" checked="checked">
I'd like to select it, by his name and value.我想 select 它,他的名字和价值。 The id is random so i can't select like this.
id 是随机的,所以我不能像这样 select 。
I don't know how to select it with: value="OUI" and name="form_fields[gym_custom_radio]"我不知道如何使用 select 它: value="OUI" 和 name="form_fields[gym_custom_radio]"
When i can select this input, i'd like to hide it then.当我可以 select 这个输入时,我想隐藏它。 It can be JS or Jquery, i tried but i can't do it, so thank you for your help !!
可以是JS或者Jquery,我试过了,还是不行,谢谢你的帮助!!
Use an Attribute Selector with multiple attributes.使用具有多个属性的属性选择器。
const radioButton = document.querySelector(
'input[name="form_fields[gym_custom_radio]"][value="OUI"]')
Make sure you quote the values so you don't run into trouble with the square-brackets in the name
.确保引用这些值,以免
name
中的方括号出现问题。
You can use the same selector string with jQuery.您可以使用与 jQuery 相同的选择器字符串。
Demo...演示...
const radioButton = document.querySelector( 'input[name="form_fields[gym_custom_radio]"][value="OUI"]') radioButton.classList.add('active') console.log('OUI data-price-inc:', radioButton.dataset.priceInc)
.active { box-shadow: 0px 0px 5px 2px red; }
<input id="tb-radio-gym_custom_radio-10ce67e3" type="radio" value="OUI" name="form_fields[gym_custom_radio]" data-price-inc="0" checked="checked"> <input id="tb-radio-gym_custom_radio-7r7r09r" type="radio" value="NON" name="form_fields[gym_custom_radio]" data-price-inc="1">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.