本文翻译自:check / uncheck checkbox using jquery? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • Setting “checked” for a checkbox with jQuery? 使用jQuery为复选框设置“选中”? 40 answers 40个答案

I have some input text fields in my page and am displaying their values using JavaScript. 我的页面中有一些输入文本字段,并使用JavaScript显示其值。

I am using .set("value","") function to edit the value, add an extra checkbox field, and to pass a value. 我使用.set("value","")函数来编辑值,添加一个额外的复选框字段,并传递一个值。

Here I want to check that if value == 1 , then this checkbox should be checked. 在这里,我想检查如果value == 1 ,则应选中此复选框。 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? 我通过使用两个div来做到这一点,但我对此感到不舒服,还有其他解决方案吗?

if(value == 1) {$('#uncheck').hide();$('#check').show();
} else{$('#uncheck').show();$('#check').hide();
}

#1楼

参考:https://stackoom.com/question/1B5sM/使用jquery检查-取消选中复选框-重复


#2楼

For jQuery 1.6+ : 对于jQuery 1.6+:

.attr() is deprecated for properties; .attr()已弃用于属性; use the new .prop() function instead as: 使用新的.prop()函数代替:

$('#myCheckbox').prop('checked', true); // Checks it
$('#myCheckbox').prop('checked', false); // Unchecks it

For jQuery < 1.6: 对于jQuery <1.6:

To check/uncheck a checkbox, use the attribute checked and alter that. 要检查/取消选中复选框,使用属性checked和改变这一点。 With jQuery you can do: 使用jQuery,您可以:

$('#myCheckbox').attr('checked', true); // Checks it
$('#myCheckbox').attr('checked', false); // Unchecks it

Cause you know, in HTML, it would look something like: 因为你知道,在HTML中,它看起来像:

<input type="checkbox" id="myCheckbox" checked="checked" /> <!-- Checked -->
<input type="checkbox" id="myCheckbox" /> <!-- Unchecked -->

However, you cannot trust the .attr() method to get the value of the checkbox (if you need to). 但是,您无法信任.attr()方法来获取复选框的值(如果需要)。 You will have to rely in the .prop() method. 您将不得不依赖.prop()方法。


#3楼

您可以根据值设置复选框的状态:

$('#your-checkbox').prop('checked', value == 1);

#4楼

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. 您可以使用prop() ,因为在jQuery 1.6之前 , .attr()方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。 As of jQuery 1.6 , the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes. 从jQuery 1.6开始.prop()方法提供了一种显式检索属性值的方法,而.attr()检索属性。

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); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <h4>This is a domo to show check box is checked if you enter value 1 else check box will be unchecked </h4> Enter a value: <input type="text" value="" class="customvalue"> <br>checkbox output : <input type="checkbox" class="customcheckbox"> 

使用jquery检查/取消选中复选框? [重复]相关推荐

  1. 使用JavaScript(jQuery或Vanilla)选中/取消选中复选框?

    本文翻译自:Check/Uncheck checkbox with JavaScript (jQuery or Vanilla)? 如何使用JavaScript,jQuery或vanilla选中/取消 ...

  2. 检查是否已使用jQuery选中复选框

    如何检查是否使用复选框数组的ID选中了复选框数组中的复选框? 我正在使用以下代码,但是无论ID为何,它始终返回已选中复选框的数量. function isCheckedById(id) {alert( ...

  3. jQuery如果选中复选框

    本文翻译自:jQuery if checkbox is checked I have a function below that I want to only trigger when a check ...

  4. jQuery实现获取选中复选框的值

    应用场景: 我们应该经常见到系统中出现列表,会有一个对列表数据的操作(如删除, 修改,查看等).我们可以在每个列表项后面加一个删除按钮,把列表项的 相关参数(如 id)post到后台进行删除.当然如果 ...

  5. jq多选按钮值_jQuery实现获取选中复选框的值实例详解

    应用场景: 我们应该经常见到系统中出现列表,会有一个对列表数据的操作(如删除, 修改,查看等).我们可以在每个列表项后面加一个删除按钮,把列表项的相关参数(如 id)post到后台进行删除.当然如果你 ...

  6. el-checkbox点击后面的内容不选中复选框

    提出需求:要求点击后面的tag前面复选框不选中 思考:把后面的内容移出checkbox标签内,尝试后发现因为循环和样式问题不可取. 查百度都是点击后面的内容选中复选框.于是想反向思考,看了Elemen ...

  7. 如何检查jQuery中是否已选中复选框?

    我需要检查复选框的checked属性,并使用jQuery根据checked属性执行操作. 例如,如果选中了年龄复选框,那么我需要显示一个文本框来输入年龄,否则隐藏该文本框. 但是以下代码默认情况下返回 ...

  8. html获取选中复选框的值,jquery获取复选框被选中的值

    JS获取复选框被选中的值 0 1 2 3 4 5 6 7 JS代码 对checkbox的其他几个操作 1. 全选 2. 取消全选 3. 选中所有奇数 4. 反选 5. 获得选中的所有值 js代码 $( ...

  9. Jquery获取选中复选框的值(checkBox)

    jquery的checkbox取值赋值选中 <%@ page language="java" import="java.util.*" pageEncod ...

最新文章

  1. HDU 5785 interesting
  2. MyBatis 与 Hibernate
  3. Bash 实例,第 2 部分
  4. C语言 | 变量的存储方式
  5. python canny检测_【数字图像分析】基于Python实现 Canny Edge Detection(Canny 边缘检测算法)...
  6. mybatis查询树形数据的两种方法
  7. 做项目的一些思路(针对小白)
  8. PDF文件在线转换HTML的方法
  9. 用QuickCHM v2.6 制作帮助文档
  10. 服务器无限刷石机推荐,【红石技巧】-世界上最快的刷石机
  11. Express(一) ——简单入门
  12. CAD的图层过滤器有什么用?
  13. Linux 字体微调 - windows 效果版
  14. 【信号处理】基于蚁群优化随机共振检测附matlab代码
  15. 分享快速检测肖特基二极管的小窍门
  16. redis安装Windows
  17. MyBatis从入门到精通(三):MyBatis XML方式的基本用法之多表查询
  18. RabbitMQ消息队列工作原理及集成使用
  19. VM虚拟机adb调试手机
  20. php curlclose,PHP curl_close函数 - PHP 教程 - 自强学堂

热门文章

  1. Android课程---Android Studio使用小技巧:提取方法代码片段
  2. 分布式文件系统MooseFs部署(二)
  3. SSH自动生成数据库
  4. layui的tree实现 struts 2+layui+jsp
  5. 软件工程的 第二天贪吃蛇
  6. Jenkins远程调度Shell命令
  7. 《构建之法》 读书笔记(6)
  8. SVN和Maven及Jenkins(转)
  9. 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n。如果输入的是奇数调用1/1+1/3+...+1/n;...
  10. Reapter 中客户端控件和服务器端控件的选择