2014-02-10 45 views
0

我有兩個單選按鈕和兩個文本框。當我點擊第一個複選框時,兩個texbox都不應該是隻讀的。但是,當我選擇第二個單選按鈕時,它應該使這兩個文本框只讀。我搜索了一下,發現了很多解決方案,但不知道爲什麼沒有人在我的代碼中工作。 HTML:使文本框只讀在javascript

<asp:RadioButton ID="mailNew" Text="New" runat="server" GroupName="mail_select" onclick="showHide(1)" ClientIDMode="Static" /><br /> 
    <asp:RadioButton ID="mailExisting" Text="Existing" runat="server" GroupName="mail_select" onclick="showHide(2)" ClientIDMode="Static" /> 

    <asp:TextBox ID="txtPromotion" runat="server" Width="77px" ></asp:TextBox><br /> 
    <asp:TextBox ID="txtSubject" runat="server" Width="288px"></asp:TextBox><br /> 

的Javascript:

function showHide(val) { 
     var txtpid=document.getElementById(<% = txtPromotion %>) 
     var txtsub= document.getElementById('<% = txtSubject.ClientID %>'); 
     if (val == 2) { 
      txtpid.readOnly = false; 
      txtsub.readOnly = false; 
     } 
     if (val == 1) { 
      txtsub.setAttribute("readOnly.true"); 
      txtpid.setAttribute("readOnly,true"); 
     } 
    } 
+0

你可以檢查'txtpid'和'txtsub'變量是否被設置?或者你在控制檯中發現任何錯誤?如果是的話請在這裏粘貼 – anurupr

+0

我沒有收到任何錯誤.. – Ami

回答

0

我看到兩個問題:

  1. 您沒有使用ClientIDtxtPromotion

  2. 您打電話給setAttribute時使用的屬性名稱不正確,根本沒有值。

只是在你的代碼一致地使用反映屬性:

function showHide(val) { 
    var txtpid=document.getElementById('<% = txtPromotion.ClientID %>') 
    var txtsub= document.getElementById('<% = txtSubject.ClientID %>'); 
    if (val == 2) { 
     txtpid.readOnly = false; 
     txtsub.readOnly = false; 
    } 
    else if (val == 1) { 
     txtsub.readOnly = true; 
     txtpid.readOnly = true; 
    } 
} 

請注意,您的代碼假設val決不會比12其他任何東西,因爲它沒有更新readOnly,除非它是一個這兩個值。考慮到這一點,這可能更清潔:

function showHide(val) { 
    document.getElementById('<% = txtPromotion.ClientID %>').readOnly = val == 1; 
    document.getElementById('<% = txtSubject.ClientID %>').readOnly = val == 1; 
} 
+0

非常感謝。..這是工作。 – Ami