Bạn đang xem: How to check and uncheck a checkbox with jquery
I am using .set("value","") function to edit the value, địa chỉ an extra checkbox field, và to pass a value.
Here I want to check that if value == 1, then this checkbox should be checked. Otherwise, it should remain unchecked.
I did this by using two divs, but I am not feeling comfortable with that, is there any other solution?
if(value == 1) $("#uncheck").hide(); $("#check").show(); else $("#uncheck").show(); $("#check").hide();
tóm tắt
Improve this question
Follow
edited Mar 11, 2019 at 23:51
Aniket G
3,36311 gold badge1111 silver badges3838 bronze badges
asked Jul 2, 2013 at 8:09
Irfan AhmedIrfan Ahmed
8,97588 gold badges3232 silver badges5353 bronze badges
0
add a phản hồi |
3 Answers 3
Sorted by: Reset to default
Trending sort available
Trending sort Trending sort is based off of the mặc định sorting method — by highest score — but it boosts votes that have happened recently, helping to lớn surface more up-to-date answers.
It falls back to lớn sorting by highest score if no posts are trending.
Switch to Trending sortHighest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
1431
For jQuery 1.6+ :
.attr() is deprecated for properties; use the new .prop() function instead as:
$("#myCheckbox").prop("checked", true); // Checks it$("#myCheckbox").prop("checked", false); // Unchecks itFor jQuery checked & alter that. With jQuery you can do:
$("#myCheckbox").attr("checked", true); // Checks it$("#myCheckbox").attr("checked", false); // Unchecks itCause you know, in HTML, it would look something like:
However, you cannot trust the .attr() method to lớn get the value of the checkbox (if you need to). You will have to rely in the .prop() method.
tóm tắt
Improve this answer
Follow
edited Jan 19, 2018 at 9:14
user3352686
answered Jul 2, 2013 at 8:12

EricEric
18.1k22 gold badges3333 silver badges3939 bronze badges
8
| Show 3 more comments
73
You can use prop() for this, as Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way lớn explicitly retrieve property values, while .attr() retrieves attributes.
var prop=false;if(value == 1) prop=true; $("#checkbox").prop("checked",prop);or simply,
$("#checkbox").prop("checked",(value == 1));Snippet
$(document).ready(function() var chkbox = $(".customcheckbox"); $(".customvalue").keyup(function() chkbox.prop("checked", this.value==1); ););This is a domo to lớn show kiểm tra box is checkedif you enter value 1 else kiểm tra box will be unchecked Enter a value:checkbox đầu ra :
mô tả
Improve this answer
Follow
edited Jan 31, 2017 at 4:35
answered Jul 2, 2013 at 8:13

Rohan KumarRohan Kumar
40k1111 gold badges7373 silver badges103103 bronze badges
địa chỉ a phản hồi |
28
You can phối the state of the checkbox based on the value:
$("#your-checkbox").prop("checked", value == 1);
mô tả
Improve this answer
Follow
answered Jul 2, 2013 at 8:13

billyonecanbillyonecan
19.8k88 gold badges4141 silver badges6262 bronze badges
2
add a phản hồi |
Not the answer you're looking for? Browse other questions tagged or ask your own question.
The Overflow Blog Featured on Meta
Linked
4477
Setting "checked" for a checkbox with jQuery
0
How khổng lồ create "check/uncheck all" with checkboxes?
1
Uncheck checkbox if AJAX error
551
What is the difference between properties và attributes in HTML?
97
How lớn reset all checkboxes using jQuery or pure JS?
4
How lớn to Check/Uncheck check-box using JavaScript or jquery?
0
How vì I uncheck checkbox when click
1
Check Box Validation
2
jQuery uncheck checkbox if disabled
0
Get value of checkbox in Rails check_box_tag using jquery ajax
See more linked questions
Related
3062
Is there an "exists" function for jQuery?
3098
How can I upload files asynchronously with jQuery?
2606
Adding a table row in jQuery
8442
How vì chưng I kiểm tra if an element is hidden in jQuery?
4477
Setting "checked" for a checkbox with jQuery
1948
Abort Ajax requests using jQuery
5050
How vì I kiểm tra whether a checkbox is checked in jQuery?
1432
check if checkbox is checked with jQuery
603
jQuery phối checkbox checked
Hot Network Questions more hot questions
lang-js
Stack Overflow
Products
Company
Stack Exchange Network
Site kiến thiết / biệu tượng công ty © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. Rev2022.9.15.42976
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.