//1、如果通过prop()函数更改<input>和<button>元素的type属性,在多数浏览器上将会抛出一个错误,因为该属性一般不允许在后期更改。
//如果使用prop()函数操作表单元素的checked、selected、disabled等属性,如果该元素被选中(或禁用),则返回true,否则(意即HTML中没有该属性)返回false。
//3、prop()函数还可以设置或返回DOM元素的Element对象上的某些属性,例如:tagName、selectedIndex、nodeName、nodeType、ownerDocument、defaultChecked和defaultSelected等属性。
//在IE9及更早版本中,如果使用prop()函数设置的属性值不是一个简单的原始值(String、Number、Boolean),并且在对应的DOM元素被销毁之前,该属性没有被移除,则可能会导致内存泄漏问题。如果你只是为了存储数据,建议你使用data()函数,以避免内存泄漏问题。
<script>
$(function () {
$("#j_cbAll").click(function () {
//如果全选按钮被选中 则所有按钮军被选中 上面有prop用法 用prop()方法解决执行一次之后不能再全选问题
$("#j_tb :checkbox").prop("checked", $(this).prop("checked"));
}); $("#j_tb :checkbox").click(function () {
singleClick();
}); $("#fanxuan").click(function () {
$("#j_tb :checkbox").each(function () {
//反选
$(this).prop("checked", !$(this).prop("checked"));
singleClick();
}); });
// 如果有一个子的checkbox没有选中,全选不选中 如果子的checkbox都选中了,全选选中
function singleClick(){
//如果子元素没有被选中 全选不选中
if(!$("#j_tb :checkbox").checked){
$("#j_cbAll").prop("checked",false);
}
//checkbox的个数
var chsub = $("#j_tb :checkbox").length;
//checkbox选中的个数
var checkedsub = $("#j_tb :checkbox:checked").length;
      //判断选择个数与所有个数是否相同
if (checkedsub === chsub) {
          //全选按钮被选中
$("#j_cbAll").prop("checked", true);
};
} });
</script>
 

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
} .wrap {
width: 300px;
margin: 100px auto 0;
} table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #c0c0c0;
width: 300px;
} th,
td {
border: 1px solid #d0d0d0;
color: #404060;
padding: 10px;
} th {
background-color: #09c;
font: bold 16px "微软雅黑";
color: #fff;
} td {
font: 14px "微软雅黑";
} tbody tr {
background-color: #f0f0f0;
} tbody tr:hover {
cursor: pointer;
background-color: #fafafa;
}
</style>
<script src="../js/jquery-1.12.4.js"></script>
</head>
<body>
<div class="wrap">
<table>
<thead>
<tr>
<th>
<input type="checkbox" id="j_cbAll" />
</th>
<th>菜名</th>
<th>饭店</th>
</tr>
</thead>
<tbody id="j_tb">
<tr>
<td>
<input type="checkbox" name="checkbox" />
</td>
<td>红烧肉</td>
<td>田老师</td>
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox" />
</td>
<td>西红柿鸡蛋</td>
<td>田老师</td>
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox" />
</td>
<td>红烧狮子头</td>
<td>田老师</td>
</tr>
<tr>
<td>
<input type="checkbox" name="checkbox" />
</td>
<td>日式肥牛</td>
<td>田老师</td>
</tr>
<tr>
<td id="fanxuan">反选</td>
</tr>
</tbody>
</table>
</div> </body>
</html>

jquery prop()方法 解决全选 不全选 反选 问题 解决执行一次不不能再执行问题的更多相关文章

  1. jQuery prop方法替代attr方法

    jquery attr()方法获取标签的 checked 会有问题,所以用了 prop() 方法.

  2. jquery全选 不全选

    <input type="checkbox" id="check">点击 <input type="checkbox" c ...

  3. jQuery prop() 方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. jquery实现全选、取消反选、加JavaScript三元运算(三种法法实现反选)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. jquery中attr方法和prop方法的区别

    关于checked的属性,最重要的概念就是你要记住,它跟checked的状态值是毫无关系的,设置checked = "checked"或者checked = "true& ...

  6. jquery attr()和prop()方法的区别

    $('').attr()返回的是html对象 $('').prop()返回的是DOM对象 attr(): attr() 方法设置或返回被选元素的属性和值. 当该方法用于返回属性值,则返回第一个匹配元素 ...

  7. JQuery常见方法

    <!DOCTYPE htmi> <html> <head> <meta charset="UTF-8"> <title> ...

  8. js prop方法

    添加和删除属性 $("button").click(function(){ var $x = $("div"); <!--添加属性--> $x.pr ...

  9. jquery操作checkbox方法(全选、全不选、至少选择一个、选择值/文本)

    原文:http://blog.csdn.net/u014079773/article/details/52371382 在实际开发中我们经常操作checkbox,不仅仅要获得checkbox选中的值, ...

随机推荐

  1. Codeforces 306B

    #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...

  2. LINQpad 用法汇总

    // C# Expression //Courses // .Join ( // CourseSections, // course => course.CourseId, // section ...

  3. php 验证身份证有效性,根据国家标准GB 11643-1999 15位和18位通用

    //验证身份证是否有效 function validateIDCard($IDCard) { if (strlen($IDCard) == 18) { return check18IDCard($ID ...

  4. Win7下unetbootin-windows-585工具制作Ubuntu12.04 U盘启动盘

    1.下载unetbootin-windows-585工具,网址如下: unetbootin-windows-585 2.unetbootin-windows-585制作U盘启动盘 准备好1个4G的U盘 ...

  5. linux 命令之 uptime

    uptime 命令是用来查询linux系统负载的. 命令格式 uptime [OPTION] -V 显示版本号 不带參数的 uptime 直接输出系统负载. 何为系统负载呢? 系统平均负载被定义为在特 ...

  6. ASP.NET Web API Authorization using Tokens

    Planning real world REST API http://blog.developers.ba/post/2012/03/03/ASPNET-Web-API-Authorization- ...

  7. android 计时器,倒计时

    自己定义CountDownTimer /** * 计时器 * @author Administrator * */ class TimeCount extends CountDownTimer{ pu ...

  8. memcached在注册表的位置

    HKEY_LOCAL_MACHINE-System-ControlSet001-services-Memcached Server

  9. javascript 中字符串之比较

    <script type="text/javascript"> var string1="apple"; var string2="Ban ...

  10. 【Howie玩docker】-docker安装

    windows忽略,小苹果木有,所以咱只看ubuntu和centOS的吧! 参考书<Docker技术入门与实战> Ubuntu 14.04安装Docker Ubuntu 14.04版本官方 ...