2011-01-13 84 views
1

现状:jQuery UI的复选框按钮 - 按钮,然后单击兼容性问题 - IE8

我有以下的jQuery:

addButton(); 

function addButton(){ 
    var div = $("#mydiv"); 
    div.append(addHtml()); 
    addEvents(div); 
} 

function addEvents(div){ 
div.find(".toggleButton").button(); 
div.find(".toggleButton").click(function(e) { 
    alert("test"); 
}); 
} 

function addHtml(div){ 
return "<input class=\"toggleButton\" type=\"checkbox\" id=\"id1\" name=\"name1\" /><label for=\"id1\">Yes</label>"; 
} 

复选框变成一个按钮,但没有信号上的警报单击。为什么按钮部分工作,但不是点击部分?

更新: 这适用于firefox,但不适用于ie-8。不幸的是,我需要它在ie-8中工作。

更新2: 如果我注释掉.button调用,click部分在ie-8中工作。我怎样才能一起工作?

更新3:在Firefox产生 实际的HTML:在IE-8产生

<input class="toggleButton ui-helper-hidden-accessible" id="id1" name="name1" type="checkbox"><label aria-disabled="false" role="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" aria-pressed="false" for="id1"><span class="ui-button-text">Yes</span></label>

实际的HTML:

<INPUT id=id1 class="toggleButton ui-helper-hidden-accessible" type=checkbox name=name1 jQuery1294922695438="218"><LABEL aria-disabled=false class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role=button aria-pressed=false for=id1 jQuery1294922695438="223"><SPAN class=ui-button-text jQuery1294922695438="225">Yes</SPAN></LABEL>

+0

哈哈,我们都在同一时间进行编辑 - 谢谢:) – kralco626 2011-01-13 12:09:28

+0

只是为了确保HTML被转义,因为它是用于回显或其他东西(即浏览器看不到\“) – 2011-01-13 12:10:01

+0

另外:Where 'div'set? – 2011-01-13 12:10:53

回答

4

问题出在IE8决定什么激活点击。在IE-8中,标签是驱动点击的标签,因此您必须将点击事件附加到标签上!

此外,您不能在输入和标签上具有相同的类;因为它在ie-8和firefox都能正常工作,所以你必须这样做:

function addEvents(div){ 
div.find(".toggleButtonInput").button(); 
div.find(".toggleButtonLabel").click(function(e) { 
    alert("test"); 
}); 
} 

function addHtml(div){ 
return "<input class=\"toggleButtonInput\" type=\"checkbox\" id=\"id1\" name=\"name1\" /><label class=\"toggleButtonLabel\" for=\"id1\">Yes</label>"; 
} 

然后它正常工作。

0

我会使用:

$(".toggleButton", div).click(function(e) { 
    alert("test"); 
}); 
1

您可以使用最快的id选择器,并使用.live()绑定click事件,因为您在运行时生成元素。

$("#id1").live("click", function(){ 
    alert("clicked"); 
});