簡體   English   中英

防止用戶單擊RadioButton

[英]Prevent users to click RadioButton

在我的表單中,我有一些TextBoxes。 更改TextBox的值后,將選擇相應的RadioButton。 但是我想防止用戶更改RadioButton的值。 我試圖像這樣禁用。

 optA.Attributes.Add("disabled", "disabled");

但是此腳本將單選按鈕顯示為灰色,效果不好。 是否有任何腳本可以防止用戶單擊/更改單選按鈕的選定值?

小例子是

totalval=document.getElementById('txt1').value+document.getElementById('txt2').value
if (totalval>101)
document.getElementById('optA1').checked=true;
else if(totalval<100)
document.getElementById('optA2').checked=true;

這就是我要的。 因此,如果正在使用,用戶不應更改單選按鈕(optA1,optA2)的值

optA1.Attributes.Add("disabled", "disabled");

我無法在服務器端獲取optA1的值。 我的aspx代碼是

<asp:RadioButton ID="optA1" GroupName="optAchieve" runat="server" Text="30" value="30" onclick="indicateColor()" />
<asp:RadioButton ID="optA2" GroupName="optAchieve" runat="server" Text="20" value="20" onclick="indicateColor()" />

嘗試通過諸如此類的onclick事件阻止用戶操作:

optA.Attributes.Add("onclick", "return false;");

您必須右鍵單擊單選按鈕上的javascript,無論選擇了什么單選選項,始終要從javascript中選擇默認選項。

附加一個只讀類,並將單選按鈕的屬性更改為每次單擊均未選中。

<input type="radio" id="radio1" name="radio" class="readonlyradio">
<input type="radio" id="radio2" name="radio" class="readonlyradio">​

在您的JS中使用此功能:

  $(".readonlyradio").click(function(){
$(this).attr('checked', false);
  });​

檢查此小提琴: http : //jsfiddle.net/Xwtvp/

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM