简体   繁体   English

单击父级时更改单选按钮 state

[英]change radio button state when parent is clicked

I have this markup:我有这个标记:

<div class='A'>
    <input type='radio' name='B' class='B' />
</div>
<div class='A'>
    <input type='radio' name='B' class='B' />
</div>
<div class='A'>
    <input type='radio' name='B' class='B' />
</div>

The desired functionality is to select the radio either by clicking parent div or the radio input itself and if the radio is already checked then click on parent will have no effect ie return false.所需的功能是 select 收音机通过单击父div或收音机输入本身,如果已选中收音机,则单击父级将无效,即返回 false。

I have made it to change on click of parent, but when i click on the radio button, nothing happens.我已经让它在点击父母时改变,但是当我点击单选按钮时,什么也没有发生。 What's wrong with my approach?我的方法有什么问题?

jQuery: jQuery:

$('input:radio').closest('div').on('click', function (e) {
    if ($('input:radio', this).prop('checked') === true) {
        console.log("returning false");
        return false;
    }
    $('input:radio', this).prop('checked', true);
    console.log("Clicked : " + $(this).attr('class'));
});

Fiddle: http://jsfiddle.net/pSSc9/1/小提琴: http://jsfiddle.net/pSSc9/1/

May I suggest using label elements instead of div s?我可以建议使用label元素而不是div吗? You'll get the same behavior and won't need javascript at all.您将获得相同的行为,并且根本不需要 javascript。 CSS will take care of the appearance. CSS 将负责外观。 I made that simple change in your fiddle, and it worked fine.我在你的小提琴中做了一个简单的改变,效果很好。

http://jsfiddle.net/jeffman/WQEDv/2/ http://jsfiddle.net/jeffman/WQEDv/2/

$('.A').on('click', function (e) {
    if ($(e.target).is('.B')) {
        e.stopPropagation();
        // So that event does not bubble when radio is selected
    } else {
        if ($('input:radio', this).prop('checked') === true) {
            console.log("returning false");
            return false;
        }
        $('input:radio', this).prop('checked', true);
    }
    console.log("Clicked : " + $(e.target).attr('class'));
});

The problem with your code was you were returning false when the checkbox is clicked.您的代码的问题是您在单击复选框时返回 false 。 So you were indirectly doing event.preventDefault() and event.stopPropagation() by doing return false;所以你间接地做event.preventDefault()event.stopPropagation()return false;

You explicitly need to set the checked property to true only when clicked on the div.只有在单击 div 时,您才明确需要将选中的属性设置为 true。 But when you click on the radio it performs the default action.但是,当您单击收音机时,它会执行默认操作。 So you need to stop the propagation of the event.所以你需要停止事件的传播。

Check Fiddle检查小提琴

DEMO演示

e.preventDefault(); disables the radio button click event禁用radio按钮click事件

$('input:radio').closest('div').on('click', function (e) {
    $('input:radio', this).prop('checked', true);
});

The click event from the radio button is bubbling up to the div , so the callback gets triggered in both cases.单选按钮的click事件正在冒泡到div ,因此在这两种情况下都会触发回调。 The problem is that you're preventing the default action which, in the case of the radio button, is it becoming checked.问题是您正在阻止默认操作,在单选按钮的情况下,它是否被选中。

What you can do is add a condition that exits the callback if the element clicked was the radio button:如果单击的元素是单选按钮,您可以添加一个退出回调的条件:

$('input:radio').closest('div').on('click', function (e) {
    if ($(e.target).is('input')) {
        return;
    }

    if ($('input:radio', this).prop('checked') === true) {
        console.log("returning false");
        return false;
    }
    $('input:radio', this).prop('checked', true);
    console.log("Clicked : " + $(this).attr('class'));
});

Working example工作示例

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM