【问题标题】:On input type=radio checked, add a class to input=text在 input type=radio 检查时,添加一个类到 input=text
【发布时间】:2018-11-06 21:46:51
【问题描述】:

如何链接单选按钮和填充的文本输入,以便在选择单选时,输入文本区域中的文本也将更改为可以说...红色粗体?

我知道逻辑是:
选中 radio-A 和 input-text-A 时,将 CSS 类添加到 input-text-A。 未选中时删除类。如果选择了 radio-B,则更改 input-text-B,依此类推...

但是现在这个简单的脚本针对所有的文本输入。

$('input[type=text]').addClass('red');
.red {
  color: red;
  font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-inline">
  <label class="" for="">
    <input class="" type="radio" name="answer-Q1" value="option1"> A. </label>
  <input type="text" name="answers" class="" placeholder="" required>
</div>
<br>
<div class="form-inline">
  <label class="">
    <input class="" type="radio" name="answer-Q2" value="option1"> B. </label>
  <input type="text" name="answers" class="" placeholder="" required>
</div>

【问题讨论】:

  • 更改无线电绑定,评估选择了哪个无线电值,相应地调整文本输入。
  • 您可以为相应的输入文本提供一个属性以仅影响一个文本字段
  • 您提供的 html sn-p 有点臭,因为每个名称只有一个单选按钮。
  • 旁注 - 我不会将类命名为 red... 您出于某种原因选择了红色以表示某些东西 - 将类命名为 原因某事。一个类应该描述什么是 或它的状态,然后你决定该状态下的事物应该是什么样子(在本例中:红色)。原因/状态不会改变,但您应用的样式/颜色可能会改变。

标签: javascript jquery html


【解决方案1】:

给出你的标记,实际上不需要添加任何类或使用 javascript,你可以用纯 CSS 做你想做的事:

input[type="radio"]:checked + input[type="text"] { 
    color: red; 
    font-weight: bold; 
}

至于如何使用 jQuery 添加类,我倾向于编写“健壮”的解决方案,这些解决方案可能会更长一些,但不会那么“脆弱”(意思是:如果标记发生了一点变化,脚本仍然可以工作) .我写这个的方式 - 假设无法控制标记 - 将使用 jQuery 的 closestfind 来定位目标文本输入:

// no-conflict-save document ready shorthand
jQuery(function($) {
     // bind to the "change" event of all inputs that are radio buttons
    jQuery('input[type="radio"]').on('change', function() {
        // find the text input
        var $text_input = $(this).closest('div').find('input[type="text"]');
        // if there isn't one, get out
        if ( ! $text_input.length ) {
            return;
        }

        // if the radio button is checked, add the class
        if ($(this).is(':checked')) {
            $text_input.addClass('red');
        } else {
            // otherwise, remove the class
            $text_input.removeClass('red');
        }
    });
});

但是,如果我确实可以控制标记,我会向无线电输入元素添加一个类,并使用它来使脚本更“通用”有用,并缩小输入的范围绑定(这将使相同的脚本也可以在复选框+文本输入上有效地工作):

// no-conflict-save document ready shorthand
jQuery(function($) {
     // bind to the "change" event of any inputs with the "watch-change" class
    jQuery('input.watch-change]').on('change', function() {
        // find the text input.  Note, this would find multiple text inputs if they existed.
        var $text_input = $(this).closest('div').find('input[type="text"]');
        // if there isn't a text input to work with, get out
        if ( ! $text_input.length ) {
            return;
        }

        // if the radio button is checked, add the class
        if ($(this).is(':checked')) {
            $text_input.addClass('red');
        } else {
            // otherwise, remove the class
            $text_input.removeClass('red');
        }
    });
});

老实说,如果对您的项目范围有更好的了解,就有可能编写出更高效、可重用的 sn-p 脚本。

【讨论】:

  • 谢谢!我只能使用 CSS 来实现这一点。我必须清理我的代码。起初它不起作用,因为每个收音机都放在一个单独的 div 中。 (我这样做是为了使用 Bootstrap 中的 'form-inline' 类。)
【解决方案2】:

这样做:

$("input[type=radio]").on("change", function(e) {
    if (e.currentTarget) {
        e.currentTarget.next("input[type=text").addClass("red");
    }
});

【讨论】:

    【解决方案3】:

    这是工作代码。

    $('input:radio').click(function() {
        $('label:has(input:radio:checked)').addClass('rightAnswer');
        $('label:has(input:radio:not(:checked))').removeClass('rightAnswer');
     
    });
    .container {margin:0 auto; margin-top:50px;}
    
    .rightAnswer {font-weight:bold; color:#2979FF;}
    
    .inputAnswers {width:200px;}
    
    .block {display:block;}
    
    input[type="radio"]:checked + input[type="text"] { 
        color: #2979FF; 
        font-weight: bold; 
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="container">
        <label class="block" for="answer-Q1A">
      <input type="radio"  class=""  name="answer-Q1"  value="1"> A.
        <input type="text" name="answers" class="inputAnswers" id="answer-Q1A" placeholder="" required></label>
       
        <label class="block" for="answer-Q1A">
      <input type="radio" class=""  name="answer-Q1"  value="1"> B.
        <input type="text" name="answers" class="inputAnswers" id="answer-Q1A" placeholder="" required></label>
        
        <label class="block" for="answer-Q1A">
      <input type="radio" class=""  name="answer-Q1"  value="1"> C.
        <input type="text" name="answers" class="inputAnswers" id="answer-Q2A" placeholder="" required></label>
    </div>

    【讨论】:

      猜你喜欢
      • 2018-05-21
      • 2019-07-10
      • 1970-01-01
      • 2013-06-28
      • 2014-03-28
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 2022-07-01
      相关资源
      最近更新 更多