gpt4 book ai didi

javascript - 购物 list 应用程序 : using checkboxes to cross off items

转载 作者:行者123 更新时间:2023-11-28 01:47:00 27 4
gpt4 key购买 nike

我正在通过 javascript/jquery 创建购物 list 应用程序,但在使用复选框来划掉列表上的项目时遇到问题。不过我已经很接近了。当选中某个框时,列表中的所有项目都会被划掉,并且应用程序顶部的主文本输入中的文本也具有删除线质量。选中其中一个框只会删除其旁边的相应文本。

这是我针对此特定功能的 JavaScript:

$('#shopping-list').on('change','input[type=checkbox]',function(){
var input = $(this).parent().siblings('input[type=text]');
$('input').css('textDecoration','line-through');

});

这里还有该应用程序当前版本的链接:

https://dl.dropboxusercontent.com/u/91499081/ShoppingList/shoppingList.html

最佳答案

简单的错误 - 我自己也犯过很多次。

jsFiddle demo

您正确识别了要加下划线的特定输入标记,并将该标记分配给变量。

但是在下一个 jQuery 行中,您将变量名称作为字符串,这告诉 jQuery 您想要获取该类型的所有元素。由于“input”是有效的元素类型,因此所有输入元素都会被修改。

因此,更改:

$('input').css('textDecoration','line-through');

致:

$(input).css('textDecoration','line-through');

我还建议选择不同的变量名称。如果您使用不同的变量名称,例如“myInput”,您可能已经找到了解决方案。

<小时/>

根据您的评论问题:

要选中/取消选中文本字段:

jsFiddle example

$('#shopping-list').on('change','input[type=checkbox]',function(){
var input = $(this).parent().find('input[type=text]');
if ($(this).is(':checked') ) {
$(input).css('textDecoration','line-through');
}else{
$(input).css('textDecoration','none');
}
});

关于javascript - 购物 list 应用程序 : using checkboxes to cross off items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20152414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com