gpt4 book ai didi

javascript - 单击时将按钮 ID 附加到输入值

转载 作者:行者123 更新时间:2023-11-28 19:31:30 25 4
gpt4 key购买 nike

如何附加 id按钮到 input单击按钮时的值?

输入示例(按下按钮):

#en #de #fr #it #es

示例输出:

<input name="langs" value="en,de,fr,it,es,">

这是我所拥有的,但它不起作用:

$(".lang-btn").click(function() {
$(this).toggleClass("active");
$("input[name=languages]").val() += $(this).attr("id") . ",";
});

然后我希望能够重新调整 input值,删除 id ,当再次按下按钮时。

最佳答案

关闭,.val() 将值作为参数:

$(".lang-btn").click(function() {
$(this).toggleClass("active");
var id = this.id,
currentValue = $("input[name=languages]").val();

//Check for existing
if (currentValue.indexOf(id) > -1) {
//remove it
currentValue = currentValue.replace(id + ",", "");
$("input[name=languages]").val(currentValue);
} else {
$("input[name=languages]").val(currentValue + id + ",");
}
});

工作演示:http://jsfiddle.net/9L76d0sr/

关于javascript - 单击时将按钮 ID 附加到输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26678829/

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