简体   繁体   English

防止用户单击RadioButton

[英]Prevent users to click RadioButton

In my form I have some TextBoxes. 在我的表单中,我有一些TextBoxes。 Once the value of TextBox is changed then corresponding RadioButton will be selected. 更改TextBox的值后,将选择相应的RadioButton。 But I want to prevent users from changing the value of RadioButton. 但是我想防止用户更改RadioButton的值。 I tried disabling like this. 我试图像这样禁用。

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

But this script greys out the radiobutton which is not looking good. 但是此脚本将单选按钮显示为灰色,效果不好。 Is there any script to prevent users from clicking/changing the selected value of radiobutton? 是否有任何脚本可以防止用户单击/更改单选按钮的选定值?

Small example is 小例子是

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;

This is what I want. 这就是我要的。 So the users should not change the value of radiobuttons (optA1,optA2) if am using 因此,如果正在使用,用户不应更改单选按钮(optA1,optA2)的值

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

I couldnt get the value of optA1 at server side. 我无法在服务器端获取optA1的值。 My aspx code is 我的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中选择默认选项。

Attach a readonly class and change attribute of radio button to unchecked on each click. 附加一个只读类,并将单选按钮的属性更改为每次单击均未选中。

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

Use this function in your JS: 在您的JS中使用此功能:

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

Check this fiddle: http://jsfiddle.net/Xwtvp/ 检查此小提琴: http : //jsfiddle.net/Xwtvp/

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM