2011-01-27 49 views
2

我使用ASP.Net MVC 1目前正在建設一個網站,並使用this JavaScript文件,以便與圖像替換默認的單選按鈕。我正在使用單選按鈕在jQuery .toggle之間切換兩種形式。IE7不調用JavaScript的onclick

這工作完全在火狐,Chrome,Safari和IE8,但不會在IE7在所有的工作。但是,如果我使用IE的開發人員工具檢查代碼並在運行時在onclick方法中添加一個空格,它就可以工作。

我不知道這是爲什麼,我已經試過jQuery的,常規的JavaScript的各種組合,使用返回false;許多變化都無濟於事。

如果任何人都可以擺脫任何光線,這將是大加讚賞。下面的相關代碼。

的Javascript:

var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active; 

    for (a = 0; a < inputs.length; a++) { 
     if (inputs[a].type == "radio" && inputs[a].className == "styled") { 
      span[a] = document.createElement("span"); 

      span[a].className = inputs[a].type; 
      span[a].setAttribute("onclick", inputs[a].getAttributeNode("onclick").nodeValue); 
      if (inputs[a].checked == true) { 
       position = "0 -" + (radioHeight * 2) + "px"; 
       span[a].style.backgroundPosition = position; 
      } 
      inputs[a].parentNode.insertBefore(span[a], inputs[a]); 
      inputs[a].onchange = Custom.clear; 
      if (!inputs[a].getAttribute("disabled")) { 
       span[a].onmousedown = Custom.pushed; 
       span[a].onmouseup = Custom.check; 
      } else { 
       span[a].className = span[a].className += " disabled"; 
      } 
     } 
    } 

代碼:

<%=Html.RadioButton("rbCustomerSelect", true,Model.IsNewCustomer.GetValueOrDefault(false),new { id="NewCustomerRadioButton", @class="styled", onclick="ShowNewCustomerForm(true);" })%> 

這是呈現爲:

<span class="radio" style="background-position: 0px -50px;" onclick="ShowNewCustomerForm(true);"/> 
+0

添加空格之前單擊收音機時是否出現JavaScript錯誤?也許在解釋時間?下一個明顯的問題是,爲什麼不只是讓服務器返回跨度而不是輸入? – 2011-01-27 03:26:19

+0

根本沒有javascript錯誤。至少沒有被抓到。 – Steve 2011-01-27 03:42:50

回答

1

你說你使用jQuery,所以我建議結合的Click事件時DOM已加載並且從單選按鈕生成了span標記。 jQuery的美妙之處在於它試圖在不同的javascript實現中保持一致性。

嘗試擺脫的onclick參數,以及改變的JavaScript,使其將所生成跨越的ID。 添加以下行:

span[a].className = inputs[a].type; 
span[a].id = inputs[a].id + "-span"; 

然後使用這個JavaScript生成

$(function(){ 
    $("#NewCustomerRadioButton-span").click(function(){ShowNewCustomerForm(true)}); 
}) 

後跨度或許IE7沒有正確註冊跨度爲DOM元素綁定到click事件,但使用jQuery綁定到單擊事件應該修復此問題。