Jquery 检查类是否存在于多个元素之一中

发布于 2024-12-28 07:16:34 字数 995 浏览 3 评论 0原文

如何检查多个 li 元素之一中是否存在类,如果有则调用函数?

嵌套的

    中有多个
    ; 列表。我需要的是检查每个 .selectbox ul 内是否是具有关联 .selected 类的许多 li a 元素之一。如果是这样,请将其值传递到 value 属性。

这是一个代码:

<div id="selectbox1" class="selectbox">
    <input type="hidden" name="selectbox1" />
    <ul>
        <li><a href="#" name="value01">Option 01</a></li>
        <li><a href="#" name="value02">Option 02</a></li>
        <li><a href="#" name="value03" class="selected">Option 03</a></li>
        <li><a href="#" name="value04">Option 04</a></li>
        <li><a href="#" name="value05">Option 05</a></li>
    </ul>
</div>

谢谢大家,我选择了最低毫秒响应时间。

How to check if there is a class in one of the many li elements, if so call a function ?

There is multiple <div class="selectbox"/> within nested <input type="hidden"/> and <ul /> list. What i need is to check if inside each .selectbox ul is one of many li a element with associated .selected class. If so, pass it's value into <input type="hidden"/> value attribute.

Here is a code:

<div id="selectbox1" class="selectbox">
    <input type="hidden" name="selectbox1" />
    <ul>
        <li><a href="#" name="value01">Option 01</a></li>
        <li><a href="#" name="value02">Option 02</a></li>
        <li><a href="#" name="value03" class="selected">Option 03</a></li>
        <li><a href="#" name="value04">Option 04</a></li>
        <li><a href="#" name="value05">Option 05</a></li>
    </ul>
</div>

Thank you guys to all of you, i choosed with the lowest ms response time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不气馁 2025-01-04 07:16:34

这应该可以做到:

$('.selectbox').each(function(){
   var $selected = $('a.selected',$(this));
    if($selected.length){
         $('input',$(this)).val($selected.text()); 
    }
});

实时示例(出于演示目的,隐藏更改为文本): http://jsfiddle.net/YL4AB/
实例2(显示多个.selectbox div):http://jsfiddle.net/ YL4AB/1/

This should do it:

$('.selectbox').each(function(){
   var $selected = $('a.selected',$(this));
    if($selected.length){
         $('input',$(this)).val($selected.text()); 
    }
});

Live example (with hidden changed to text for demo purpose): http://jsfiddle.net/YL4AB/
Live example 2 (Showing multiple .selectbox div's): http://jsfiddle.net/YL4AB/1/

愁杀 2025-01-04 07:16:34

我会通过获取“selectbox”div 下的所有 input 元素的集合,然后查找选定的 a (如果任何)。例如:

$(".selectbox > input").each(function() {
    this.value = $(this).closest(".selectbox").find("a.selected").first().attr("name") || "";
});

实例,我将隐藏输入设置为文本输入,以便您可以看到它。单击该按钮来触发上述代码。 (单击链接会更改选定的链接,但不会更新输入 - 因此您可以看到单击按钮时会发生什么。)我还使用 CSS 来突出显示选定的链接。

I'd do it by getting a set of all of the input elements under the "selectbox" div and then finding the selected a (if any). E.g.:

$(".selectbox > input").each(function() {
    this.value = $(this).closest(".selectbox").find("a.selected").first().attr("name") || "";
});

Live example, I made the hidden input a text input so you could see it. Click the button to fire the above code. (Clicking the links changes which one is selected, but doesn't update the input -- so you can see what happens when you click the button.) I also used CSS to highlight the selected one.

自我难过 2025-01-04 07:16:34
$('input[name="selectbox1"]').val($("#selectbox1 li > a.selected").text());
$('input[name="selectbox1"]').val($("#selectbox1 li > a.selected").text());
自由如风 2025-01-04 07:16:34

这是一些代码,解释了它的作用:

// Grab the selectbox elements
$('.selectbox').each(function() {
    // Loop through the LI elements which have Anchor elements with the '.selected' class
    $(this).find('li a.selected').each(function() {
        // Execute arbitrary function
        alert($(this).text());
    });
});

它非常简单,并且符合大多数其他人所说的内容。希望文档能有所帮助。

Here's some code, explaining what it does:

// Grab the selectbox elements
$('.selectbox').each(function() {
    // Loop through the LI elements which have Anchor elements with the '.selected' class
    $(this).find('li a.selected').each(function() {
        // Execute arbitrary function
        alert($(this).text());
    });
});

It's pretty simple, and fits in with what most everyone else has said. Hopefully the documentation will help out a bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文