如何从不匹配的兄弟姐妹那里获取文本?

时间:2021-06-17 15:17:51

I am creating an A/B test variant using VWO. The website has a list with checkboxes laid out like so;

我正在使用VWO创建A / B测试变体。该网站有一个列表,其复选框如此布局;

<ol>
    <li>
        <label>
            <input class="checkBox" type="checkbox">
            <p class="checkBoxAnc">Text to grab</p>
        </label>
    </li>
</ol>

There is an apply button, when this is clicked I want it to cycle through all of the inputs. If checked is true then I need to grab the text from the class "checkBoxAnc" (p element) and concatenate it to a variable.

有一个应用按钮,当单击它时,我希望它循环所有输入。如果checked为true,那么我需要从类“checkBoxAnc”(p元素)中获取文本并将其连接到变量。

I have tried the following:

我尝试过以下方法:

var self= $(this);
//This is referring to the input that the user has clicked, so class '.checkBox'

self.next() // This doesn't work as element's do not match

self.nextUntil('.checkBoxAnc') // Same issue as .next()

var checkBoxSibling = self.parent().find('.checkBoxAnc').text();
// This returns an empty string

When trying to find the parent type this is being returned as 'undefined' rather than 'label'

在尝试查找父类型时,这将返回为“undefined”而不是“label”

Are there any other techniques to access '.checkBoxAnc'?

有没有其他技术可以访问'.checkBoxAnc'?

1 个解决方案

#1


0  

Something like this...

这样的东西......

var foo = '';

$('input[type=checkbox]:checked').map(function() {
    foo += $(this).next().text(); // value your looking for
}).get();

https://jsfiddle.net/cmac_/8w7vghbt/

#1


0  

Something like this...

这样的东西......

var foo = '';

$('input[type=checkbox]:checked').map(function() {
    foo += $(this).next().text(); // value your looking for
}).get();

https://jsfiddle.net/cmac_/8w7vghbt/