If the checkbox is checked, then I only need to get the value as 1; otherwise, I need to get it as 0. How do I do this using jQuery?
$("#ans").val()
will always give me one right in this case:
<input type="checkbox" id="ans" value="1" />
If the checkbox is checked, then I only need to get the value as 1; otherwise, I need to get it as 0. How do I do this using jQuery?
|
|||||
|
Use |
||||
|
will tell you if it's checked. You can also use a second parameter true/false to check/uncheck the checkbox.
Per comment, use
|
|||||||||
|
Just use It returns a boolean value. |
||||
|
|
|||||||||
|
I've found the same problem before, hope this solution can help you. first, add a custom attribute to your checkboxes:
write a jQuery extension to get value:
use code to get check-box value:
you can test here |
|||||||||||||
|
Stefan Brinkmann's answer is excellent, but incomplete for beginners (omits the variable assignment). Just to clarify:
It works like this:
Example:
Alerts 'no' If this is helpful, please upvote Stefan Brinkmann's answer. |
|||
|
Try this :)
|
|||
|
You can also use:
|
||||
|
|
|||
|
Jquery :
In your function:
|
|||
|
Use:
|
|||||||||
|
statusNum will equal 1 if the checkbox is checked, and 0 if it is not. EDIT: You could add the DOM to the function as well.
|
||||
|
There are Several options are there like....
If all these option return true then you can set value accourdingly. |
|||||
|
I've came through a case recently where I've needed check value of checkbox when user clicked on button. The only proper way to do so is to use
this worked in my case maybe someone will need it. When I've tried |
|||
|
try this
Here flag is true if checked otherwise false
Again this will make cheked
|
||||
|
You can get value (true/false) by these two method
|
|||
|
If you want integer value of checked or not try: $("#ans:checked").length |
|||
|