2010-10-19 58 views
0

下面是從C#.NET動態生成HTML樣本: -jQuery的 - 複選框標籤點擊不工作

<ul class="multiselect-checkboxes clearfix horizontal" id="chkMC"><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices0-931336440" id="ctl03_ctl05_CheckboxChoices0-931336440"><label for="ctl03_ctl05_CheckboxChoices0-931336440">1-2 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices1-1671570667" id="ctl03_ctl05_CheckboxChoices1-1671570667"><label for="ctl03_ctl05_CheckboxChoices1-1671570667">5-10 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices2679786207" id="ctl03_ctl05_CheckboxChoices2679786207"><label for="ctl03_ctl05_CheckboxChoices2679786207">10-12 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices3-977631492" id="ctl03_ctl05_CheckboxChoices3-977631492"><label for="ctl03_ctl05_CheckboxChoices3-977631492">12-15 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices41547758987" id="ctl03_ctl05_CheckboxChoices41547758987"><label for="ctl03_ctl05_CheckboxChoices41547758987">15-20 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices5-1658326960" id="ctl03_ctl05_CheckboxChoices5-1658326960"><label for="ctl03_ctl05_CheckboxChoices5-1658326960">20-21 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices6-100787876" id="ctl03_ctl05_CheckboxChoices6-100787876"><label for="ctl03_ctl05_CheckboxChoices6-100787876">22-23 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices7122075492" id="ctl03_ctl05_CheckboxChoices7122075492"><label for="ctl03_ctl05_CheckboxChoices7122075492">23-24 Years</label></span></li><li onclick="setMcCb(event);"><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoices8-1742375481" id="ctl03_ctl05_CheckboxChoices8-1742375481"><label for="ctl03_ctl05_CheckboxChoices8-1742375481">More than that</label></span></li><li><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoicesOther" id="ctl03_ctl05_CheckboxChoicesOther"><label for="ctl03_ctl05_CheckboxChoicesOther">other</label></span>&nbsp;&nbsp;<input type="text" style="width: 150px;" class="QTextBox" id="ctl03_ctl05_otherText" name="ctl03$ctl05$otherText"></li><li><span class="QRadio"><input type="checkbox" name="ctl03$ctl05$CheckboxChoicesNA" id="ctl03_ctl05_CheckboxChoicesNA"><label for="ctl03_ctl05_CheckboxChoicesNA">plplpl</label></span></li></ul> 

JS方法: -

function setMcCb(e) { 
       if (!e) var e = window.event;        
       var tg = (window.event) ? e.srcElement : e.target; //FF & IE. 
       var j$chk = j$(tg).closest('li').find(':checkbox'); 

       if (j$chk[0] != tg)       
        j$chk.attr('checked', !j$chk.attr('checked')); 
       } 

* 的目的JS方法: -當有人點擊複選框,標籤或li標籤時,我創建了這個javascript,複選框應該切換。 *

* 我的問題: - 當我點擊標籤元素,js不工作。切換沒有發生。請幫我細化js代碼。 *

我做它的工作如下代碼: -

if (!e) var e = window.event;        
        var tg = (window.event) ? e.srcElement : e.target; //FF & IE.      
        //Label click does not toggle the cbx with this JS, so the below processing is skipped by returning false. 
        if (tg.tagName.toUpperCase() == 'LABEL') 
         return false; 

        var j$chk = j$(tg).closest('li').find(':checkbox'); 
        if (j$chk[0] != tg){       
         if(j$chk.attr('checked')) 
          j$chk.removeAttr('checked'); 
         else 
          j$chk.attr('checked', 'checked'); 
         } 

回答

3

您的問題是在下面的行

j$chk.attr('checked', !j$chk.attr('checked')); 

通話到!j$chk.attr('checked')將返回一個布爾值,然後指定要檢查的值。您應該嘗試:

if(j$chk.attr('checked')) { 
    j$chk.removeAttr('checked'); 
} else { 
    j$chk.attr('checked', 'checked'); 
} 
+0

夥計們,現在標籤上的點擊沒有按照上面的代碼進行切換。我想對所有三個元素複選框,標籤和LI項目進行切換。 – Karan 2010-10-19 11:47:31

+0

我觀察到的一件事是,其他部分(j $ chk.attr('checked','checked'); )中的代碼在Label中不起作用。這很奇怪。 – Karan 2010-10-19 11:58:36

+0

我發現的另一件事是,當單擊標籤以將其設置爲未選中時,該複選框甚至不會被取消選中。 – Karan 2010-10-19 12:09:18

1

而是嘗試

if (j$chk[0] != tg)  
    if(!j$chk.attr('checked')){ 
     j$chk.attr('checked', 'checked'); 
    }else { 
     j$chk.removeAttr('checked'); 
    } 
} 
+0

不起作用。如果選中此框,則只會設置已選中的屬性。 – EMMERICH 2010-10-19 10:39:23

+0

那是你想要的嗎?點擊標籤上的複選框,如果未選中,則取消選中複選框。對? – 2010-10-19 10:44:20

+0

是的,但是當你的代碼已經具有'checked'屬性時(意味着它已經被檢查過!),你的代碼只會設置'checked'屬性 – EMMERICH 2010-10-20 07:19:53