I have two radio buttons within an HTML form. A dialog box appears when one of the fields is null. How can I check whether a radio button is selected?
标签:
javascript
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I just want to ensure something gets selected (using jQuery):
Note this behavior wit jQuery when getting radio input values:
So
$('input[name="myRadio"]').val()
does not return the checked value of the radio input, as you might expect -- it returns the first radio button's value.You can use this simple script. You may have multiple radio buttons with same names and different values.
An example:
Just trying to improve on Russ Cam's solution with some CSS selector sugar thrown in with the vanilla JavaScript.
No real need for jQuery here, querySelectorAll is widely supported enough now.
Edit: fixed a bug with the css selector, I've included the quotes, although you can omit them, in some cases you can't so it's better to leave them in.
With JQuery, another way to check the current status of the radio buttons is to get the attribute 'checked'.
For Example:
In this case you can check the buttons using: