防止用户更改单选按钮的状态 [英] preventing a user from changing the state of a radio button

查看:73
本文介绍了防止用户更改单选按钮的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图阻止用户使用jquery或javascript更改单选按钮的状态.我可以使用Disabled属性来做到这一点,但是我不喜欢灰色外观.我正在向用户显示我的模型之一的正确/错误状态,所以我不希望他们能够更改它.根据模型,组中只有一个单选按钮处于选中状态(true)或未选中状态(false),因此他们无法取消选中该按钮,但可以检查并更改单选按钮的状态.单选按钮不会以表单的形式提交,因此仅用于从数据库/模型中读取,但是如果将其更改为数据库中实际的相反值,将给用户带来糟糕的体验.这是我对jquery和html结构的最新尝试.

I am trying to prevent a user from changing the state of a radio button using jquery or javascript. I could do this with the disabled property but I don't like the grayed out look. I am showing the user a true/false state of one of my models, so I don't want them to be able to change it. There is only one radio button in the group that is either checked (true) or unchecked (false) based on the model, so they can't uncheck it but they can check and change the radio button state. The radio button doesn't get submitted in a form so it is only for reading from the database/model but it would be a bad user experience if they changed it to the opposite value that is actually in the database. Here is my latest attempt at the jquery and html structure.

<input type="radio" checked="checked">

<input type="radio">

和jquery

$(document).ready(function () {
    $(input[type = "radio"]).change(function () {
        $(this).removeAttr("checked");
    });
});

推荐答案

尝试一下

$(document).ready(function () {
    $('input[type = "radio"]').change(function () {
        return false;
    });
});

这篇关于防止用户更改单选按钮的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆