如何通过JQuery删除复选框的文本

Ark*_*Ark 0 html checkbox jquery

我的代码是:

$(function(){

    $('input:checkbox').on('change','span',function(){
        var input= $(this).find('span');
        if($(this).is(':checked')){
            //alert($(this).name);
            $(input).css('textDecoration','line-through');
        }else{
            $(input).css('textDecoration','none');
        }

    })
})
Run Code Online (Sandbox Code Playgroud)

这是我的HTML

 <div><input  type="checkbox" id="a1"></input><span>This is ok</span></div><br/>
    <div><input type="checkbox" id="a2" ></input><span>This is yes</span></div>
Run Code Online (Sandbox Code Playgroud)

但它没有用.我想我没有得到正确的输入.提前致谢.

Aru*_*hny 6

您需要定位更改处理程序的checkbox元素,然后span是下一个兄弟

$(function () {
    $('input:checkbox').on('change', function () {
        var input = $(this).next('span');
        if (this.checked) {
            $(input).css('textDecoration', 'line-through');
        } else {
            $(input).css('textDecoration', 'none');
        }
    })
})
Run Code Online (Sandbox Code Playgroud)

演示:小提琴


如果你想要事件授权

$(function () {
    $(document).on('change', 'input:checkbox', function () {
        var input = $(this).next('span');
        if (this.checked) {
            $(input).css('textDecoration', 'line-through');
        } else {
            $(input).css('textDecoration', 'none');
        }
    })
})
Run Code Online (Sandbox Code Playgroud)

演示:小提琴