[英]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.