javascript - jQuery - Select Element Where Value Equals - Stack Overflow

I'm trying to find a simple way to check a radio option using jQuery based on its initial value at

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
Add a ment  | 

2 Answers 2

Reset to default 3

Simple 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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744969151a4603861.html

相关推荐

  • javascript - jQuery - Select Element Where Value Equals - Stack Overflow

    I'm trying to find a simple way to check a radio option using jQuery based on its initial value at

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信