如何使用 jQuery 将toggleClass 添加到选中的复选框?

发布于 2024-10-08 03:14:20 字数 850 浏览 0 评论 0原文

我在单击时附加了一些输入文本字段和复选框。 但是,当选中复选框时,我想将Class切换到输入文本字段。 这是我的创建代码:

jQuery(function() {
var counter = 0;

$('#appendbtn').click(function() {
    var elems = '<div>' + '<input type="checkbox" id="checkbox"' + (counter) + '" class="item"/>' + '<input type="text" id="inputfield"' + (counter) + '"/>' + '</div>';
    $('#box').append(elems);
    counter++;
    return false;
});

这就是应该发生的事情;

if ($("#checkbox").is(":checked"))
 {
$("inputfield" + counter).toggleClass("complete")
 });

当然这是不正确的,但我希望你明白我的意思;) 顺便问一下,如果没有 if 语句,这可能吗?

这是 jsfiddle http://www.jsfiddle.net/gkdKY/

哦,这正是我想要 http://www.jsfiddle.net/xzmcL/ 但带有附加的复选框!

I append some input textfields and checkboxes onclick.
But when a checkbox is checked i want to toggleClass to the input textfield.
This is my create code:

jQuery(function() {
var counter = 0;

$('#appendbtn').click(function() {
    var elems = '<div>' + '<input type="checkbox" id="checkbox"' + (counter) + '" class="item"/>' + '<input type="text" id="inputfield"' + (counter) + '"/>' + '</div>';
    $('#box').append(elems);
    counter++;
    return false;
});

This is what should happen;

if ($("#checkbox").is(":checked"))
 {
$("inputfield" + counter).toggleClass("complete")
 });

Ofcourse this isn't correct, but i hope you get what i mean ;)
is this by the way possible without an if statement?

here is the jsfiddle http://www.jsfiddle.net/gkdKY/

oh and this is exactly what i want http://www.jsfiddle.net/xzmcL/
but with appended checkboxes!

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

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

发布评论

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

评论(2

呢古 2024-10-15 03:14:20

这一行:

var elems = '<div>' + '<input type="checkbox" id="checkbox"' + (counter) + '" class="item"/>' + '<input type="text" id="inputfield"' + (counter) + '"/>' + '</div>';

...有两个额外的引号。它应该是:

var elems = '<div>' + '<input type="checkbox" id="checkbox' + (counter) + '" class="item"/>' + '<input type="text" id="inputfield' + (counter) + '"/>' + '</div>';

这会破坏 click 事件的选择器。但请注意,这无论如何都行不通,因为您试图挂钩一个在按下“单击我”链接之前不存在的元素 - 因此您需要使用 .live().delegate() 无论如何。

像这样的事情可能会起作用:

$("#drag").delegate("input[type=checkbox]", "change", function(){
   var index = this.id.substring(8);
   $("#inputfield" + index).toggleClass("complete");
});

This line:

var elems = '<div>' + '<input type="checkbox" id="checkbox"' + (counter) + '" class="item"/>' + '<input type="text" id="inputfield"' + (counter) + '"/>' + '</div>';

...has two extra quotes. It should be:

var elems = '<div>' + '<input type="checkbox" id="checkbox' + (counter) + '" class="item"/>' + '<input type="text" id="inputfield' + (counter) + '"/>' + '</div>';

That breaks your selector for the click event. But note, that won't work anyway, since you are trying to hook an element that does not exist until the 'click me' link is pressed - so you need to use .live() or .delegate() for that anyway.

Something like this might work:

$("#drag").delegate("input[type=checkbox]", "change", function(){
   var index = this.id.substring(8);
   $("#inputfield" + index).toggleClass("complete");
});
初见终念 2024-10-15 03:14:20

您想要:

$('input:checkbox').click(function(){
   $('#inputfield').toggleClass('bold');
});

我假设您没有动态地将复选框的选中状态设置为 true 。

You want:

$('input:checkbox').click(function(){
   $('#inputfield').toggleClass('bold');
});

I'm assuming you aren't setting your checkboxes' checked state to true dynamically.

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