jqeury判斷和操作checkbox

以下是jqeury判斷checkbox是否選中的方法作彤,親測(cè)第一種和第二種有效淀衣,第三種如果沒有定義checked屬性昙读,報(bào)錯(cuò)undefind

  • alert($("#allCache").get(0).checked)

  • alert($('#allCache').is(':checked'))

  • alert ($('#allCache').attr('checked'))

選中和取消選中checkbox
javascript中:

  • 取消選中
    document.getElementById("allCache").checked = false;
  • 選中
    document.getElementById("allCache").checked = true;

親測(cè)有效,可反復(fù)選中和不選中

jquery中:

  • 重復(fù)點(diǎn)擊失效
//測(cè)試發(fā)現(xiàn)膨桥,第一次有效蛮浑,但是經(jīng)過一次選中和不選中之后唠叛,就無效了
if($('#allCache').is(':checked')){
    $('#allCache').removeAttr("checked");
}else{
    $('#allCache').attr("checked",true);
}
  • 可重復(fù)操作
//可以重復(fù)操作
if($('#allCache').is(':checked')){
//    document.getElementById("allCache").checked = false;
//    $('#allCache').removeAttr("checked");
        $("#allCache").prop('checked',false);
}else{
//    document.getElementById("allCache").checked = true;
//    $('#allCache').attr("checked",true);
    $("#allCache").prop('checked',true);
}

總結(jié):

??? jquery中上面兩種方案涉及到attr和prop的區(qū)別。

先看attr的解釋:Get the value of an attribute for the first element in the set of matched elements.
?? The .attr() method gets the attribute value for only the first element in the matched set. To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method.

在看prop的解釋:Get the value of a property for the first element in the set of matched elements.
?? The .prop() method gets the property value for only the first element in the matched set. It returns undefined for the value of a property that has not been set, or if the matched set has no elements. To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method.

Attributes vs. Properties

The difference between attributes and properties can be important in specific situations. 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 to explicitly retrieve property values, while .attr() retrieves attributes.

For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the [.prop()](http://api.jquery.com/prop/) method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.

Concerning boolean attributes, consider a DOM element defined by the HTML markup <input type="checkbox" checked="checked" />, and assume it is in a JavaScript variable named elem:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末沮稚,一起剝皮案震驚了整個(gè)濱河市艺沼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蕴掏,老刑警劉巖障般,帶你破解...
    沈念sama閱讀 222,946評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異盛杰,居然都是意外死亡挽荡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門即供,熙熙樓的掌柜王于貴愁眉苦臉地迎上來定拟,“玉大人,你說我怎么就攤上這事募狂“焖兀” “怎么了?”我有些...
    開封第一講書人閱讀 169,716評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵祸穷,是天一觀的道長(zhǎng)性穿。 經(jīng)常有香客問我,道長(zhǎng)雷滚,這世上最難降的妖魔是什么需曾? 我笑而不...
    開封第一講書人閱讀 60,222評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮祈远,結(jié)果婚禮上呆万,老公的妹妹穿的比我還像新娘。我一直安慰自己车份,他們只是感情好谋减,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著扫沼,像睡著了一般出爹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上缎除,一...
    開封第一講書人閱讀 52,807評(píng)論 1 314
  • 那天严就,我揣著相機(jī)與錄音,去河邊找鬼器罐。 笑死梢为,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播铸董,決...
    沈念sama閱讀 41,235評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼祟印,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了袒炉?” 一聲冷哼從身側(cè)響起旁理,我...
    開封第一講書人閱讀 40,189評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎我磁,沒想到半個(gè)月后孽文,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡夺艰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評(píng)論 3 343
  • 正文 我和宋清朗相戀三年芋哭,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片郁副。...
    茶點(diǎn)故事閱讀 40,926評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡减牺,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出存谎,到底是詐尸還是另有隱情拔疚,我是刑警寧澤,帶...
    沈念sama閱讀 36,580評(píng)論 5 351
  • 正文 年R本政府宣布既荚,位于F島的核電站稚失,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏恰聘。R本人自食惡果不足惜句各,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望晴叨。 院中可真熱鬧凿宾,春花似錦、人聲如沸兼蕊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)孙技。三九已至产禾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間绪杏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工纽绍, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蕾久,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,368評(píng)論 3 379
  • 正文 我出身青樓拌夏,卻偏偏與公主長(zhǎng)得像僧著,于是被迫代替她去往敵國(guó)和親履因。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容