如何使用JQuery基于单选按钮启用/禁用文本框

时间:2018-03-27 03:07:57

标签: jquery asp.net-mvc

我有2个单选按钮和2个文本框。我想基于单选按钮启用/禁用文本框。首次加载页面时,应禁用txtB。只有在选中相应的单选按钮B时才会启用它。你能指导我怎么做吗?

JQuery的

 $("document").ready(function () {
           $('#M').on('change', '.ABC input[type = "radio"]', function () {
                    var divA = $('.P').find('#A'); 

                    var lblA = $('.panel-heading').find('#lblA'); 

                    var id = $(this).val();              
                    if (id == "A")
                    {

                        $(lblB).hide();
                        $(lblA).show();
                    }


                   $('#' + id).show();

               });
        });

       $("document").ready(function () {
           $('#M').on('change', '.CDE input[type = "radio"]', function () {                           
                    var lblA = $('.panel-heading').find('#lblA'); 
                    var lblB = $('.panel-heading').find('#lblB'); 

                    var id = $(this).val();              
                    if (id == "B")
                    {
                        $(divA).hide();

                        $(lblB).show();
                    }

               });
        });

单选按钮

<div class="ABC">
                <label>
                    @Html.RadioButtonFor(m => m.Name, "A", new { 
                       @checked = "checked" })<span>A</span>
                </label>
           </div>
       <div class="CDE">
           @Html.RadioButtonFor(m => m.Name, "B")<span>B</span>
       </div>

文本框

    <div class="P">
        <div id="A" class="container">
            @Html.TextBoxFor(m => m.txtA)
         </div>

    </div>     

    <div id="B">
      @Html.TextBoxFor(m => m.txtB)                         
     </div> 

2 个答案:

答案 0 :(得分:1)

您可以使用解决方案

$('input[type="radio"]').change(function(){
  $('#' + $(this).val())
    .removeAttr('disabled')
    .siblings('input[type="text"]')
    .attr('disabled', 'disabled');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" value="text1" checked name="textbox">Textbox 1
<input type="radio" value="text2" name="textbox">Textbox 2

<br/>

<input type="text" id="text1" />
<input type="text" id="text2" disabled/>

希望这会对你有所帮助。

<强>详细信息: 将启用哪个单选按钮,相应的文本框将被启用。

<强>假设: 你的文本框是兄弟姐妹

在您的方案中,

$('input[type="radio"]').on('change', function() {
  $('#' + $(this).find('span').text())
    .find('input[type="text"]')
    .removeAttr('disabled')
    .closest('div')
    .siblings('div')
    .find('input[type="text"]')
    .attr('disabled', 'disabled')
});

答案 1 :(得分:1)

使用txtB属性禁用disabled

<div class="ABC">
     <label>
         @Html.RadioButtonFor(m => m.Name, "A", new
         {
             @checked = "checked"
         })<span>A</span>
     </label>
</div>
<div class="CDE">
        @Html.RadioButtonFor(m => m.Name, "B")<span>B</span>
</div>

<div class="P">
    <div id="A" class="container">
        @Html.TextBoxFor(m => m.txtA)
     </div>
</div>
<div id="B">
        @Html.TextBoxFor(m => m.txtB, new
        {
               // Make this textbox disabled as you need this textbox to be disabled on page load
               @disabled = "disabled" 
        })
</div> 

使用以下jquery,

// Fire on radio button checked event    
$('input[type="radio"]').change(function () {

    // Get the value of currently clicked radio button
    var value = $(this).val();

    // Find the corresponding textbox inside the div that has the id same as radio button value and remove it disabled attribute
    $('#' + $(this).val())
    .children('input[type="text"]')
    .removeAttr('disabled');

    // Find all radio button and find its corresponding textbox and make it disable except the checked radio button.
    $('input[type="radio"]').each(function () {
        if(this.value != value)
        {
            $('#' + this.value)
            .children('input[type="text"]')
            .attr('disabled', 'disabled');
        }
    })
});