更改已选复选框的类文本。

huangapple go评论64阅读模式
英文:

jQuery, change class text of checked checkboxes

问题

Here's the translated code part you requested:

<div>
    <span class="wrapper">
        <input type="checkbox" value="" id="nr1" name="X">
        <label for="X">
            <span class="Y">
                <span class="title">Text nr 1</span>
            </span>
        </label>
    </span>
</div>
$('#submit').on("click", function(){
   $("input:checkbox:checked").each(function() {
      $(this).find(".title").text("Checked");
   });  
});

If you have any further questions or need assistance with this code, please let me know.

英文:

My HTML is generated by the website and I can't change this:

&lt;div&gt;
    &lt;span class=&quot;wrapper&quot;&gt;
        &lt;input type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;nr1&quot; name=&quot;X&quot;&gt; 
        &lt;label for=&quot;X&quot;&gt;
            &lt;span class=&quot;Y&quot;&gt;
                &lt;span class=&quot;title&quot;&gt;Text nr 1&lt;/span&gt;
            &lt;/span&gt;
        &lt;/label&gt;
    &lt;/span&gt;
&lt;/div&gt;

I'm trying to use jQuery to change the text in &lt;span class=&quot;title&quot;&gt; when checkbox id=nr1 is checked. (I have many of these checkboxes with different IDs and different texts and I want to change the text always to the same outcome when the box is checked.

I have made this jQuery code:

$(&#39;#submit&#39;).on(&quot;click&quot;, function(){
   $(&quot;input:checkbox(:checked)&quot;).each(function() {
      $(this).find( $(&quot;.title&quot;).text(&quot;Checked&quot;));
   });  
});

But it doesn't seem to select the correct object as I can't get the output back correctly. it seems to do "nothing" (I can't enable errors in the system I'm working on so I can't see why it would not work).

Can someone point me in the right direction?

答案1

得分: 3

你需要包括 .next(),因为 .find() 搜索后代元素,而 span 不是复选框的后代,但它是 DOM 中下一个元素的后代。

此外,你的示例的正确选择器语法是 $(&quot;input:checkbox:checked&quot;),而不是 $(&quot;input:checkbox(:checked)&quot;)

注意,label 的 for 属性应该与另一个元素的 ID 匹配,而不是 name

英文:

You need to include .next(), as .find() searches descendants, and the span isn't a descendant of the checkbox, but it is a descendant of the next element in the DOM.

Also, the proper selector syntax for your example is $(&quot;input:checkbox:checked&quot;), not $(&quot;input:checkbox(:checked)&quot;)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#submit&#39;).on(&quot;click&quot;, function() {
  $(&quot;input:checkbox:checked&quot;).each(function() {
    $(this).next().find($(&quot;span.title&quot;)).text(&quot;Checked&quot;);
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div&gt;
    &lt;span class=&quot;wrapper&quot;&gt;
        &lt;input type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;nr1&quot; name=&quot;X&quot;&gt; 
        &lt;label for=&quot;nr1&quot;&gt;
            &lt;span class=&quot;Y&quot;&gt;
                &lt;span class=&quot;title&quot;&gt;Text nr 1&lt;/span&gt;
            &lt;/span&gt;
        &lt;/label&gt;
    &lt;/span&gt;
&lt;/div&gt;&lt;div&gt;
    &lt;span class=&quot;wrapper&quot;&gt;
        &lt;input type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;nr2&quot; name=&quot;X&quot;&gt; 
        &lt;label for=&quot;nr2&quot;&gt;
            &lt;span class=&quot;Y&quot;&gt;
                &lt;span class=&quot;title&quot;&gt;Text nr 1&lt;/span&gt;
            &lt;/span&gt;
        &lt;/label&gt;
    &lt;/span&gt;
&lt;/div&gt;&lt;div&gt;
    &lt;span class=&quot;wrapper&quot;&gt;
        &lt;input type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;nr3&quot; name=&quot;X&quot;&gt; 
        &lt;label for=&quot;nr3&quot;&gt;
            &lt;span class=&quot;Y&quot;&gt;
                &lt;span class=&quot;title&quot;&gt;Text nr 1&lt;/span&gt;
            &lt;/span&gt;
        &lt;/label&gt;
    &lt;/span&gt;
&lt;/div&gt;

&lt;button id=&quot;submit&quot;&gt;
submit
&lt;/button&gt;

<!-- end snippet -->

Note that the label's for attribute should match the ID of another element, not the name.

> The value of the for attribute must be a single id for a labelable
> form-related element in the same document as the <label> element

答案2

得分: 2

以下是翻译好的内容:

一瞥之间,我看到两个问题:

  1. 这不太像一个查找操作:$(this).find( $(&quot;.title&quot;).text(&quot;Checked&quot;));
  2. 即使纠正了,你也找不到.title input:checkbox(:checked) 内部,因为它不是该元素的子元素。

第一个问题只是更熟悉 jQuery 语法和函数。第二个问题需要理解 DOM 遍历。与其查找复选框元素内部,不如遍历到一个共同的父级(例如 span.wrapper),然后在该元素内部找到目标。

例如:

$('#submit').on("click", function(){
   $("input:checkbox(:checked)").each(function() {
      $(this).closest('.wrapper').find('.title').text('Checked');
   });  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
    <span class="wrapper">
        <input type="checkbox" value="" id="nr1" name="X"> 
        <label for="X">
            <span class="Y">
                <span class="title">Text nr 1</span>
            </span>
        </label>
    </span>
</div>
<button id="submit">Submit</button>

请注意,这是代码的翻译部分,不包括代码本身。

英文:

At a glance I see two issues:

  1. This doesn't make much sense as a find operation: $(this).find( $(&quot;.title&quot;).text(&quot;Checked&quot;));
  2. Even when corrected, you won't find .title within input:checkbox(:checked) because it's not a child of that element.

The first issue is just more familiarity with jQuery syntax and functions. The second requires an understanding of DOM traversal. Rather than looking within the checkbox element, traverse up to a common parent (e.g. span.wrapper) then then find the target within that element.

For example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#submit&#39;).on(&quot;click&quot;, function(){
   $(&quot;input:checkbox(:checked)&quot;).each(function() {
      $(this).closest(&#39;.wrapper&#39;).find(&#39;.title&#39;).text(&#39;Checked&#39;);
   });  
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div&gt;
    &lt;span class=&quot;wrapper&quot;&gt;
        &lt;input type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;nr1&quot; name=&quot;X&quot;&gt; 
        &lt;label for=&quot;X&quot;&gt;
            &lt;span class=&quot;Y&quot;&gt;
                &lt;span class=&quot;title&quot;&gt;Text nr 1&lt;/span&gt;
            &lt;/span&gt;
        &lt;/label&gt;
    &lt;/span&gt;
&lt;/div&gt;
&lt;button id=&quot;submit&quot;&gt;Submit&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月20日 04:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定