admin管理员组文章数量:1406182
I'm trying to find a simple way to check a radio option using jQuery based on its initial value at render time.
<form>
<input type="radio" name="myRadio" value="weekly" />Weekly <br />
<input type="radio" name="myRadio" value="monthly" />Monthly <br />
<input type="radio" name="myRadio" value="yearly" />Yearly <br />
</form>
And the Javascript:
$('input [value="weekly"]').prop("checked", true);
I have a JS fiddle set up here: /
The interesting thing is that it works using the native browser document.querySelector but it doesn't seem to work using jQuery. I'd like to stick with jQuery's way of doing this to ensure consistency in the code.
Also, the reason I want to select by value is that the JSON API I'm calling returns an enumerated string type. It would be really annoying if I had to add a unique ID attribute to each radio option and then use a switch case to select the appropriate button based on the enumerated string type.
I'm trying to find a simple way to check a radio option using jQuery based on its initial value at render time.
<form>
<input type="radio" name="myRadio" value="weekly" />Weekly <br />
<input type="radio" name="myRadio" value="monthly" />Monthly <br />
<input type="radio" name="myRadio" value="yearly" />Yearly <br />
</form>
And the Javascript:
$('input [value="weekly"]').prop("checked", true);
I have a JS fiddle set up here: http://jsfiddle/xpvt214o/448712/
The interesting thing is that it works using the native browser document.querySelector but it doesn't seem to work using jQuery. I'd like to stick with jQuery's way of doing this to ensure consistency in the code.
Also, the reason I want to select by value is that the JSON API I'm calling returns an enumerated string type. It would be really annoying if I had to add a unique ID attribute to each radio option and then use a switch case to select the appropriate button based on the enumerated string type.
Share Improve this question asked Jul 19, 2018 at 17:30 ImagineEverything_BradImagineEverything_Brad 472 silver badges7 bronze badges 2-
4
'input [value="weekly"]'
take out the space in the selector. Just like in css, a space denotes a descendant. – Taplar Commented Jul 19, 2018 at 17:32 - 4 A simple thing like this is close-able as a typo. – Taplar Commented Jul 19, 2018 at 17:34
2 Answers
Reset to default 3Simple remove the space between your selector and the attribute:
$('input[value="weekly"]').prop("checked", true);
$('input[value="weekly"]').prop("checked", true);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="radio" name="myRadio" value="weekly" />Weekly <br />
<input type="radio" name="myRadio" value="monthly" />Monthly <br />
<input type="radio" name="myRadio" value="yearly" />Yearly <br />
</form>
With space, you are telling jQuery to select elements that have [value="weekly"]
but are descendants of an input
element
$(function(){
$('input[type="radio"][value="weekly"]').prop('checked', true);
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="radio" name="myRadio" value="weekly" />Weekly <br />
<input type="radio" name="myRadio" value="monthly" />Monthly <br />
<input type="radio" name="myRadio" value="yearly" />Yearly <br />
</form>
本文标签: javascriptjQuerySelect Element Where Value EqualsStack Overflow
版权声明:本文标题:javascript - jQuery - Select Element Where Value Equals - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744969151a2635128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论