使用jquery禁用文本框?

时间:2009-10-30 09:53:55

标签: jquery

我有三个具有相同名称和不同值的单选按钮。当我单击第三个单选按钮时,复选框和文本框将被禁用。但是当我选择其他两个单选按钮时,它必须显示。我需要Jquery中的帮助谢谢你......

<form name="checkuserradio">
    <input type="radio" value="1" name="userradiobtn" id="userradiobtn"/> 
    <input type="radio" value="2" name="userradiobtn" id="userradiobtn"/>
    <input type="radio" value="3" name="userradiobtn" id="userradiobtn"/>
    <input type="checkbox" value="4" name="chkbox" />
    <input type="text" name="usertxtbox" id="usertxtbox" />
</form>

11 个答案:

答案 0 :(得分:201)

HTML

<span id="radiobutt">
  <input type="radio" name="rad1" value="1" />
  <input type="radio" name="rad1" value="2" />
  <input type="radio" name="rad1" value="3" />
</span>
<div>
  <input type="text" id="textbox1" />
  <input type="checkbox" id="checkbox1" />
</div>

的Javascript

  $("#radiobutt input[type=radio]").each(function(i){
    $(this).click(function () {
        if(i==2) { //3rd radiobutton
            $("#textbox1").attr("disabled", "disabled"); 
            $("#checkbox1").attr("disabled", "disabled"); 
        }
        else {
            $("#textbox1").removeAttr("disabled"); 
            $("#checkbox1").removeAttr("disabled"); 
        }
      });

  });

答案 1 :(得分:27)

不是必需的,但对o.k.w.代码的一个小改进会使函数调用更快(因为你在函数调用之外移动条件)。

$("#radiobutt input[type=radio]").each(function(i) {
    if (i == 2) { //3rd radiobutton
        $(this).click(function () {
            $("#textbox1").attr("disabled", "disabled");
            $("#checkbox1").attr("disabled", "disabled");
        });
    } else {
        $(this).click(function () {
            $("#textbox1").removeAttr("disabled");
            $("#checkbox1").removeAttr("disabled");
        });
    }
});

答案 2 :(得分:21)

这个帖子有点旧,但应该更新信息。

http://api.jquery.com/attr/

  

要检索和更改DOM属性,例如已选中,已选中,   或禁用表单元素的状态,使用.prop()方法。

$("#radiobutt input[type=radio]").each(function(i){
$(this).click(function () {
    if(i==2) { //3rd radiobutton
        $("#textbox1").prop("disabled", true); 
        $("#checkbox1").prop("disabled", true); 
    }
    else {
        $("#textbox1").prop("disabled", false); 
        $("#checkbox1").prop("disabled", false);
    }
  });
});

答案 3 :(得分:11)

我不确定为什么其中一些解决方案会使用.each() - 这不是必需的。

这是一些工作代码,如果单击第3个复选框则禁用,否则将删除已禁用的属性。

注意:我在复选框中添加了一个ID。此外,请记住,文档中的ID必须是唯一的,因此要么删除单选按钮上的ID,要么使它们唯一

$("input:radio[name='userradiobtn']").click(function() {
    var isDisabled = $(this).is(":checked") && $(this).val() == "3";
    $("#chkbox").attr("disabled", isDisabled);
    $("#usertxtbox").attr("disabled", isDisabled);
});

答案 4 :(得分:8)

我知道回答一个老问题并不是一个好习惯,但我会把这个答案给那些后来会看到这个问题的人。

现在改变JQuery状态的最佳方法是通过

$("#input").prop('disabled', true); 
$("#input").prop('disabled', false);

请查看此链接以获取完整说明。 Disable/enable an input with jQuery?

答案 5 :(得分:2)

我会做得稍微不同

 <input type="radio" value="1" name="userradiobtn" id="userradiobtn" />   
 <input type="radio" value="2" name="userradiobtn" id="userradiobtn" />    
 <input type="radio" value="3" name="userradiobtn" id="userradiobtn" class="disablebox"/>   
 <input type="checkbox" value="4" name="chkbox" id="chkbox" class="showbox"/>    
 <input type="text" name="usertxtbox" id="usertxtbox" class="showbox" />   

注意类属性

 $(document).ready(function() {      
    $('.disablebox').click(function() {
        $('.showbox').attr("disabled", true);           
    });
});

这种方式你需要添加更多的单选按钮,你不必担心改变javascript

答案 6 :(得分:0)

我猜你可能想要将“点击”事件绑定到多个单选按钮,读取点击的单选按钮的值,并根据值,禁用/启用复选框和/或文本框。

function enableInput(class){
    $('.' + class + ':input').attr('disabled', false);
}

function disableInput(class){
    $('.' + class + ':input').attr('disabled', true);
}

$(document).ready(function(){
    $(".changeBoxes").click(function(event){
        var value = $(this).val();
        if(value == 'x'){
            enableInput('foo'); //with class foo
            enableInput('bar'); //with class bar
        }else{
            disableInput('foo'); //with class foo
            disableInput('bar'); //with class bar
        }
    });
});

答案 7 :(得分:0)

很抱歉在聚会上这么晚,但我觉得这里还有一些改进空间。不关心&#34;禁用文本框&#34;,但是放射箱选择和代码简化,使其更具未来性,以便以后进行更改。

首先,你不应该使用.each()并使用索引指出一个特定的单选按钮。如果您使用一组动态单选按钮,或者之后添加或删除某些单选按钮,那么您的代码将对错误按钮做出反应!

接下来,但是当制作OP时,情况可能并非如此,我更喜欢使用.on(&#39;点击&#39;,function(){...})而不是点击。 .. http://api.jquery.com/on/

但同样重要的是,通过选择基于其名称的单选按钮(但已经出现在帖子中),代码可以变得更加简单和未来证明。

所以我最终得到了以下代码。

HTML(基于o.k.w的代码)

<span id="radiobutt">
    <input type="radio" name="rad1" value="1" />
    <input type="radio" name="rad1" value="2" />
    <input type="radio" name="rad1" value="3" />
</span>
<div>
    <input type="text" id="textbox1" />
    <input type="checkbox" id="checkbox1" />
</div>

<强> JS-代码

$("[name='rad1']").on('click', function() {
    var disable = $(this).val() === "2";
    $("#textbox1").prop("disabled", disable); 
    $("#checkbox1").prop("disabled", disable); 
});

答案 8 :(得分:0)

MVC 4 @ Html.CheckBox一般人们想要检查和取消选中mvc复选框。

<div class="editor-field">
    @Html.CheckBoxFor(model => model.IsAll, new { id = "cbAllEmp" })
</div>

您可以为要更改的控件定义id,并在javascript中执行以下操作

<script type="text/javascript">
    $(function () {
        $("#cbAllEmp").click("", function () {
            if ($("#cbAllEmp").prop("checked") == true) {
                    $("#txtEmpId").hide();
                    $("#lblEmpId").hide();
                }
                else {
                    $("#txtEmpId").show();
                    $("#txtEmpId").val("");
                    $("#lblEmpId").show();
             }
        });
    });

你也可以像

一样更改属性
$("#txtEmpId").prop("disabled", true); 
$("#txtEmpId").prop("readonly", true); 

答案 9 :(得分:0)

$(document).ready(function () {
   $("#txt1").attr("onfocus", "blur()");
});

&#13;
&#13;
$(document).ready(function () {
  $("#txt1").attr("onfocus", "blur()");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<input id="txt1">
&#13;
&#13;
&#13;

答案 10 :(得分:0)

获取单选按钮值,如果为3,则匹配每个按钮值,然后禁用checkbox and textbox

$("#radiobutt input[type=radio]").click(function () {
    $(this).each(function(index){
    //console.log($(this).val());
        if($(this).val()==3) { //get radio buttons value and matched if 3 then disabled.
            $("#textbox_field").attr("disabled", "disabled"); 
            $("#checkbox_field").attr("disabled", "disabled"); 
        }
        else {
            $("#textbox_field").removeAttr("disabled"); 
            $("#checkbox_field").removeAttr("disabled"); 
        }
      });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span id="radiobutt">
  <input type="radio" name="groupname" value="1" />
  <input type="radio" name="groupname" value="2" />
  <input type="radio" name="groupname" value="3" />
</span>
<div>
  <input type="text" id="textbox_field" />
  <input type="checkbox" id="checkbox_field" />
</div>

相关问题