2017-07-28 111 views
0

我的JavaScript工作正常,雖然單選按鈕在單擊時不會顯示爲選中狀態。當我從我的網頁和alt + tab退回時,它會在單選按鈕周圍顯示一個陰影圓圈,但仍然沒有正確的檢查填充。爲什麼是這樣?當調用JavaScript函數onClick時,ASP單選按鈕不會選擇

我希望這是一個簡單的修復。我更新了我的asp單選按鈕來調用JavaScript函數,因爲我的ASP代碼隱藏發佈了回發並清除了我的頁面上的一些控件。不用說,使用功能背後的asp c#代碼,單選按鈕在單擊時正確檢查。當點擊JavaScript功能時,我只需看看單選按鈕上的檢查。這裏是我的無線電控制HTML:

<div class="col s12 form-section" id="crimeInsurance"> 
      <h4 class="section-heading">CRIME INSURANCE</h4> 
      <p> 
       <asp:RadioButton ID="radLLCrimeIns" runat="server" GroupName="CrimeInsurance" ClientIDMode="Static" CssClass="with-gap" OnClick="return CrimeInsuranceToggle();" AutoPostBack="true" text="ACCEPT Crime Insurance (fee of $250.00)" /> 
      </p> 
      <p> 
       <asp:RadioButton ID="radLLNOCrimeIns" runat="server" GroupName="CrimeInsurance" ClientIDMode="Static" CssClass="with-gap" OnClick="return CrimeInsuranceToggle();" AutoPostBack="true" text="DO NOT ACCEPT Crime Insurance" /> 
      </p> 
     </div> 

,這裏是我的JavaScript方法:

function CrimeInsuranceToggle() { 
     //controls the crime insurance radio yes/no buttons to update crime insurance fees 
     var oCrimeRte = document.getElementById("lblCrimeCovFlatRate"); 
     var oCrimeTtl = document.getElementById("lblCrimeCovAmtDue"); 
     var oCrimeY = document.getElementById("radLLCrimeIns"); 
     var oCrimeN = document.getElementById("radLLNoCrimeIns"); 

     var radios = document.getElementsByTagName('input'); 
     var value; 
     for (var i = 0; i < radios.length; i++) { 
      if (radios[i].type === 'radio' && radios[i].checked) { 
       //find each radio that is checked, and act on the ID 
       value = radios[i].value; 
       if (value == 'radLLCrimeIns') { 
        oCrimeTtl.innerHTML = oCrimeRte.innerHTML; 
        radios[i].checked = true; 
       } 
       else if (value == 'radLLNOCrimeIns') { 
        oCrimeTtl.innerHTML = "0.00"; 
        radios[i].checked = true; 
       } 
      } 
     } 

     return false; 
    } 

幫助是非常讚賞。

+0

在任何消息瀏覽器開發人員工具控制檯,當你點擊其中一個單選按鈕時? –

+0

你可以發佈那些asp:RadioButton控件輸出的實際HTML嗎? –

回答

1

您的函數總是返回false,從而阻止單選按鈕被檢查。

嘗試這樣的事情(未測試):

標記(注意,我通過在元件參照功能):

<div class="col s12 form-section" id="crimeInsurance"> 
    <h4 class="section-heading">CRIME INSURANCE</h4> 
    <p> 
    <asp:RadioButton ID="radLLCrimeIns" runat="server" GroupName="CrimeInsurance" ClientIDMode="Static" CssClass="with-gap" OnClick="return CrimeInsuranceToggle(this);" AutoPostBack="true" text="ACCEPT Crime Insurance (fee of $250.00)" /> 
    </p> 
    <p> 
    <asp:RadioButton ID="radLLNOCrimeIns" runat="server" GroupName="CrimeInsurance" ClientIDMode="Static" CssClass="with-gap" OnClick="return CrimeInsuranceToggle(this);" AutoPostBack="true" text="DO NOT ACCEPT Crime Insurance" /> 
    </p> 
</div> 

使用Javascript:

function CrimeInsuranceToggle(rdo) { 
    //controls the crime insurance radio yes/no buttons to update crime insurance fees 
    var oCrimeRte = document.getElementById("lblCrimeCovFlatRate"); 
    var oCrimeTtl = document.getElementById("lblCrimeCovAmtDue"); 

    //find each radio that is checked, and act on the ID 
    if (rdo.id == 'radLLCrimeIns') { 
    oCrimeTtl.innerHTML = oCrimeRte.innerHTML; 
    } else if (rdo.id == 'radLLNOCrimeIns') { 
    oCrimeTtl.innerHTML = "0.00"; 
    } 

    return rdo.checked; 
}