2011-11-04 24 views
2

在我的表單中我有一個CheckBox,並且我有一個CheckBox的標籤。在IE9中,CheckBox change event按預期工作,但在IEIE7中,點擊標籤而不是複選框時觸發事件。單擊標籤時,複選框「更改」事件起作用。在ie8中,ie7

我的HTML

<div class="item-container"> 
    <div class="label-container"> 
    </div> 
    <div class="textbox-container"> 
     <input id="AddNewProductCategory" class="" type="checkbox" width="20px" height="20px" 
      value="1" name="addnewproductcategory" tabindex="1900" /> 
    </div> 
    <div class="after-checkbox-label"> 
     <label class="Verdana11-424039" for="AddNewProductCategory"> 
      Add New Service Category</label> 
    </div> 
</div> 

我的jQuery

jq('#AddNewProductCategory').change(function(){ 
    jq('#CategoryName').next('label[for=CategoryName]').remove(); 
    jq('#Category').next('label[for=Category]').remove(); 

    if (!jq('#AddNewProductCategory').is(':checked')) { 
     alert("in change"); 
     jq('input[name="newcategory"]').val(''); 
     jq('#CategoryName').hide();  
     jq('#store_category').hide(); 
     jq('#Category').removeAttr('disabled'); 

    } 
    else { 
     jq('#Category').attr('disabled', 'disabled'); 
     jq('#CategoryName').show();  
     jq('#store_category').show(); 
    } 
}); 
+0

你有什麼版本的jQuery? –

+0

jquery versoin 1.3.2 –

+6

根據此文檔http://api.jquery.com/change/: 「從jQuery 1.4開始,Internet Explorer中的更改事件泡泡,與其他現代瀏覽器中的事件一致。」 考慮更改jquery版本。 –

回答

8

只是爲了將來參考,從我記得, 「變」 的事件是在舊版本的IE有點馬車。

您必須使用IE中的propertychange事件才能使其正常工作。我喜歡用的代碼是:

編輯2(回核心)

$(element).on(navigator.userAgent.match(/msie/i) ? "propertychange" : "change", function(evt) { 
    evt.preventDefault(); 
    // Your code here 
}); 

編輯1(越野車,不可靠)

$(element).on(!$.support.changeBubbles ? "propertychange" : "change", function(evt) { 
    evt.preventDefault(); 
    // Your code here 
}); 

此代碼已被更新後與jQuery 1.9+一起使用,並在評論中提供了來自Marius的一些輸入。

原件(不建議使用)

下面是使用舊版本的JQ那些舊代碼:

$(element).bind($.browser.msie ? 'propertychange' : 'change', function(evt) { 
    evt.preventDefault(); 
    // Your code here 
}); 
+0

對於那些仍然需要支持IE8的人來說,即使在2013年,我發現這個問題在IE9及以上版本中不會發生。所以我用'($ .browser.msie && $ .browser.version <9)'來檢查。 –

+1

$ .browser現在在jquery 1.9 +中已棄用。你可以用if(!$。support.changeBubbles)替換它。 –

+0

感謝Marius,我一直在尋找這行代碼的更新 - 我擁有的最好的(簡單)'/msie/.test(navigator.userAgent.toLowerCase())''。 – ninty9notout

相關問題