简体   繁体   English

如何使用 Javascript 通过其值和名称属性访问输入字段的值

[英]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.

相关问题 如何使用名称属性查找输入字段的值? - How to find value of input field using name attribute? 如何使用JavaScript更改输入名称属性值 - How to change Input Name Attribute Value using JavaScript 如何使用 JavaScript 添加另一个名称值递增的输入字段? - How to add another input field with increment of name value using JavaScript? 如何使用Javascript / Jquery动态添加输入字段的名称属性 - How to add name attribute of input field dynamically using Javascript/Jquery 如何更改 <input> JavaScript实时显示字段的VALUE属性? - How to change an <input> field's VALUE attribute in real time with JavaScript? 如何在输入名称中使用特殊字符访问javascript中的表单输入值? - How to access form input value in javascript with special characters in input name? 在 javascript 中使用 getElementsByTagName 获取输入字段的值和名称? - Get the value and name of input field using getElementsByTagName in javascript? 如何使用javascript选择具有srcset属性值的图像元素? - How to select image elements with its srcset attribute value using javascript? 如何使用javascript将xml属性及其值提取为json格式 - How to extract xml attribute and its value into json format using javascript 如何将JavaScript值连接到HTML输入“名称”字段? - How To Concatenate JavaScript Value to HTML Input 'name' Field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM