简体   繁体   English

jQuery Li>复选框父级

[英]jquery li > checkbox parent

I've been researching in stackoverflow and api.jquery for longer than 1h and I decided to ask because there is something I am missing here and I don't know what is it. 我一直在研究stackoverflow和api.jquery超过1小时,我决定问一下,因为这里缺少某些东西,我不知道它是什么。

<ul>
    <li>
        <input type="checkbox" **checked**>
        FIRST
    </li>
    <li>
        <input type="checkbox">
        SECOND
    </li>
    <li>
        <input type="checkbox">
        THIRD
    </li>
</ul>

It is more or less, like that. 差不多那样。 I have let the First input 'checked' by default for the test. 默认情况下,我已对“ First”输入进行了“检查”。 I would like to relate the checkbox with its closest li (for future Remove-function). 我想将复选框与其最接近的li关联(以用于将来的Remove-function)。 And this is my impossible part. 这是我不可能的部分。 I have tried .parent() .closest .prev() but it all gives me nothing (nothing that I would like). 我已经尝试过.parent() .closest .prev()但这一切都给我什么(我什么都不想)。 That's my js code, the one I am testing: 那就是我正在测试的js代码:

if($('input').prop('checked')) {
    $(this).parent('li').css('color','red');
}

The this has nothing to do with the checked input in your code. this与代码中已检查的输入无关。 It is most likely the window object, it does not magically get the context of what you selected above. 它很可能是窗口对象,它不能神奇地获得您在上面选择的内容的上下文。

Other issue is you are not looking for the checked input, you are selecting all of the inputs and when you read the prop, it is selecting the first input from the list, it is not checking them all for a checked input. 另一个问题是您不查找选中的输入,而是选择所有输入,并且在阅读道具时,它是从列表中选择第一个输入,而不是将所有选中的输入都选中。

So you need to change your selector, you need to use each and now the this would be the input element that is checked. 因此,您需要更改选择器,需要使用每个选择器,现在this将是选中的输入元素。

$('input:checked').each(function(){
    $(this).parent('li').css('color','red');
});

And this needs to be run on document ready or after the elements are rendered on the page. 这需要在准备好文档的页面上或在页面上呈现元素之后运行。 And there is no need for the each, just showing you the this, you can just chain the calls. 并且不需要每个,只需向您显示此内容,您就可以链接呼叫。

$('input:checked').parent('li').css('color','red');

And please learn about the label element. 并且请了解标签元素。

First, let's look at your code: 首先,让我们看一下您的代码:

// `.prop` does not select elements,
// it returns a property of an element,
// therefore whether the first <input>
// on the page is checked.
if($('input').prop('checked')) {
    // `this` likely does not refer to anything useful
    // Usually, it's only safe to use `$(this)` in `.each`
    // and event handlers.
    $(this).parent('li').css('color','red');
}

I think you want something like this ( check it out here ): 我认为您想要这样的东西( 在此处查看 ):

// Query for all <input>s which are checked
$('input:checked')
// Select their <li> parents
.parent('li')
// Apply CSS (or do whatever you want, e.g. remove())
.css('color','red');

First, if you are trying to check the condition on the document.ready, you'll have to find that input which is already selected, without giving the name parameter, checked will not work properly. 首先,如果您要检查document.ready上的条件,则必须找到已选择的输入,而没有提供name参数,checked将无法正常工作。

your above code will work after giving some event handlers. 您的上述代码在提供了一些事件处理程序后将起作用。

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

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