简体   繁体   English

如果选中了输入单选,则更改父div

[英]Change parent div if input radio is checked

I have 3 radio inputs, that are wrapped in div so that they look like buttons instead of the default radio circle input, on click I am replacing the button text for checked icon. 我有3个单选框,它们包装在div中,使它们看起来像按钮,而不是默认的单选框输入。单击时,我将替换选中图标的按钮文本。 Now on validation if it fails I am returning the old value, and would like to again replace the text of the button for icon checked if the radio input was checked. 现在进行验证,如果失败,我将返回旧值,并且如果检查了单选输入,则想再次替换用于检查图标的按钮的文本。

This is the script for the click action: 这是单击操作的脚本:

$('.Image-input__input-wrapper').click(function() {
      $( '.plan-text-icon-toggle' ).replaceWith( '<span class="plan-text-icon-toggle">This is what I need</span>' )
      $(this).find( '.plan-text-icon-toggle' ).replaceWith( '<span class="plan-text-icon-toggle"><i class="ion-checkmark-round"></i></span>' );
  });

This is the html: 这是html:

<div class="Image-input__input-wrapper">
    <span class="plan-text-icon-toggle">This is what I need</span>
    <input class="Image-input__input" type="radio" name="plan" value="player" {{ old('plan')=="player" ? 'checked='.'"'.'checked'.'"' : '' }}>
</div>

Now I would like to to do same for on page load if the button was checked already, but not sure how to do it? 现在,如果按钮已被选中,我想对页面加载执行相同的操作,但不确定如何执行?

Update I have tried with adapting the suggestion in the answers: 更新我尝试调整答案中的建议:

    function checkinput(elem) {
    var parent = elem.parent(), 
        checked = elem.is(':checked');

    $('.radio').removeClass('active').html('This is what I need');

    if (checked) {
        parent.addClass('active').html('Checked');
    }
}

// apply style on change
$('[type=radio]').on('change', function () {
    var elem = $(this);
    checkinput(elem);
});

// apply style on load
var elem = $('[type=radio]:checked');
checkinput(elem);

Here is the full example. 是完整的示例。 But it is not working. 但这是行不通的。

You can always use input radio and just change the style with CSS. 您始终可以使用输入单选按钮,而只需使用CSS更改样式即可。 Look the example bellow: 看下面的例子:

 function checkinput(elem) { var parent = elem.parent(), checked = elem.is(':checked'); $('.radio').removeClass('active'); if (checked) { parent.addClass('active'); } } // apply style on change $('[type=radio]').on('change', function () { var elem = $(this); checkinput(elem); }); // apply style on load var elem = $('[type=radio]:checked'); checkinput(elem); 
 .radio .on { display: none; } .radio.active .off { display: none; } .radio.active .on { display: inline; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <label class="radio"> <span class="on">Message when checked</span> <span class="off">Message when unchecked</span> <input type="radio" name="my-radio" value="1"> </label> </div> <div> <label class="radio"> <span class="on">Message when checked</span> <span class="off">Message when unchecked</span> <input type="radio" name="my-radio" value="2"> </label> </div> <div> <label class="radio"> <span class="on">Message when checked</span> <span class="off">Message when unchecked</span> <input type="radio" name="my-radio" value="3" checked> </label> </div> 

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

相关问题 如果选中无线电输入,则更改父 div 的类 - Change class of parent div if radio input checked 选中单选按钮时更改父 div 的背景 - Change background of the parent div when radio button is checked 更改收音机输入名称 - 检查消失收音机 - Change radio input name - disappear radio checked 检查输入广播后如何滚动到div - how to scroll to a div after checked input radio jQuery 将无线电输入滚动到 div 以默认选中 - jQuery scroll radio input into div to checked by default 如果检查了输入无线电,​​则更改父级的颜色:无法使其与 jQuery 一起使用 - if an input radio is checked change the parent's color: can't make it work with jQuery 在 React 中,有没有办法在选中的 Radio Input 上更改父 Label 标签的背景颜色? - In React Is there a way to change the background color of a parent Label tag on a Radio Input checked? 选中单选按钮时将类添加到父 div,如果选中其他单选按钮则删除类 - Add class to parent div when radio button is checked and remove class if other radio is checked 根据检查的输入使用 javascript 更改父样式 - Change parent style with javascript based on checked input 根据已检查的单选输入更改表单操作和文本输入名称 - Change Form Action and Text Input Name based on Radio Input Checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM