最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jquery checkbox無(wú)法用attr()二次勾選問(wèn)題的解決方法

 更新時(shí)間:2016年07月22日 09:49:33   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇jquery checkbox無(wú)法用attr()二次勾選問(wèn)題的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

今晨,漂亮的測(cè)試妹妹提了個(gè)奇怪的bug,說(shuō)我一功能checkbox時(shí)隱時(shí)現(xiàn),比如第一次打開有勾選,第n次打開可能就不選了。

想到與美女有親密接觸機(jī)會(huì),馬上雞動(dòng)起來(lái)。

經(jīng)過(guò)偶層層抽次剝繭(da da jiang you),終于知道了原因:attr()在二次選中勾選框時(shí),失效。

比如,如下HTML頁(yè)面,一點(diǎn)【選中】、二點(diǎn)【取消選中】、三點(diǎn)【選中】,瞧,不行了唄。

1.html

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>prop demo</title>
 <style>
 img {
  padding: 10px;
 }
 div {
  color: red;
  font-size: 24px;
 }
 </style>
 <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
 <input type="checkbox" checked="checked">
 <input type="checkbox">
 <input type="checkbox">
 <input type="checkbox" checked="checked">
 
<script>
$( "input[type='checkbox']" ).prop( "checked", function( i, val ) {
 return !val;
});
</script>
 
</body>
</html>

解決方案,是使用prop()替換attr()方法(在Jquery1.6以上),如下代碼:

2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Attr checked</title>
<script type="text/javascript" src="./js/jquery-1.11.2.js"></script>
<script type="text/javascript">
  function switchChecked(flag) {
    $("input[type='checkbox']").prop('checked', flag);
  }
</script>
</head>
<body>
  <input type="checkbox" />
  <input type="button" onclick="switchChecked(true)" value="選中">
  <input type="button" onclick="switchChecked(false)" value="取消選中">
</body>
</html>

關(guān)于官方文檔,見:http://api.jquery.com/attr/

或者h(yuǎn)ttp://api.jquery.com/prop/

摘抄如下:“As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.”

以上這篇jquery checkbox無(wú)法用attr()二次勾選問(wèn)題的解決方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

鸡泽县| 乐业县| 新乐市| 新安县| 新野县| 小金县| SHOW| 三江| 澄迈县| 无棣县| 吉林市| 贵定县| 若尔盖县| 许昌市| 永州市| 中阳县| 安庆市| 和政县| 广饶县| 九江市| 颍上县| 河津市| 资源县| 南岸区| 慈溪市| 南丹县| 从化市| 临澧县| 扬州市| 旺苍县| 麻城市| 家居| 蓬莱市| 营山县| 潼南县| 东阳市| 鞍山市| 仪征市| 芜湖县| 乌兰浩特市| 五指山市|