英文:
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:
<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>
I'm trying to use jQuery to change the text in <span class="title">
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:
$('#submit').on("click", function(){
$("input:checkbox(:checked)").each(function() {
$(this).find( $(".title").text("Checked"));
});
});
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 中下一个元素的后代。
此外,你的示例的正确选择器语法是 $("input:checkbox:checked")
,而不是 $("input:checkbox(:checked)")
。
注意,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 $("input:checkbox:checked")
, not $("input:checkbox(:checked)")
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('#submit').on("click", function() {
$("input:checkbox:checked").each(function() {
$(this).next().find($("span.title")).text("Checked");
});
});
<!-- language: lang-html -->
<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="nr1">
<span class="Y">
<span class="title">Text nr 1</span>
</span>
</label>
</span>
</div><div>
<span class="wrapper">
<input type="checkbox" value="" id="nr2" name="X">
<label for="nr2">
<span class="Y">
<span class="title">Text nr 1</span>
</span>
</label>
</span>
</div><div>
<span class="wrapper">
<input type="checkbox" value="" id="nr3" name="X">
<label for="nr3">
<span class="Y">
<span class="title">Text nr 1</span>
</span>
</label>
</span>
</div>
<button id="submit">
submit
</button>
<!-- 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
以下是翻译好的内容:
一瞥之间,我看到两个问题:
- 这不太像一个查找操作:
$(this).find( $(".title").text("Checked"));
- 即使纠正了,你也找不到
.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:
- This doesn't make much sense as a find operation:
$(this).find( $(".title").text("Checked"));
- Even when corrected, you won't find
.title
withininput: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 -->
$('#submit').on("click", function(){
$("input:checkbox(:checked)").each(function() {
$(this).closest('.wrapper').find('.title').text('Checked');
});
});
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论