Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击时将按钮ID附加到输入值_Javascript_Jquery - Fatal编程技术网

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

Javascript 单击时将按钮ID附加到输入值,javascript,jquery,Javascript,Jquery,如何在单击按钮时将按钮的id附加到输入值上 输入示例(按下按钮): #en#de#fr\it\es 示例输出: 这就是我所拥有的,但它不起作用: $(".lang-btn").click(function() { $(this).toggleClass("active"); $("input[name=languages]").val() += $(this).attr("id") . ","; }); 然后,当再次按下按钮时,我希望能够重新调整输入值,删除id。关闭,.va

如何在单击按钮时将按钮的
id
附加到
输入值

输入示例(按下按钮):

#en
#de
#fr
\it
\es

示例输出:

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

$(".lang-btn").click(function() {
    $(this).toggleClass("active");
    $("input[name=languages]").val() += $(this).attr("id") . ",";
});
然后,当再次按下按钮时,我希望能够重新调整
输入值,删除
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 + ",");
    }
});
工作演示:

关闭,
.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 + ",");
    }
});
工作演示:

关闭,
.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 + ",");
    }
});
工作演示:

关闭,
.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 + ",");
    }
});

工作演示:

如果您有单选按钮,您可以看到是否选中了:document.getElementById(“X”).checked=true;

然后,您可以根据需要在输入字段中附加它们。如果您有单选按钮,您可以查看是否选中了:document.getElementById(“X”)。checked=true;

然后,您可以根据需要在输入字段中附加它们。如果您有单选按钮,您可以查看是否选中了:document.getElementById(“X”)。checked=true;

然后,您可以根据需要在输入字段中附加它们。如果您有单选按钮,您可以查看是否选中了:document.getElementById(“X”)。checked=true;

然后,您可以根据需要在输入字段中附加它们

应该
this.id,
类似于
$(this.id)因为我无法使此代码正常工作?非常感谢JSFIDLE或类似的工具。@mpdc--Nope,
这是指本机DOM元素。谢谢。除再次按下按钮时删除值外,我的操作正常。同样的代码。嗯…@mpdc——我做了一个编辑来修复它,行
currentValue=currentValue.replace(id+“,”,”)惊人,一定错过了那个。非常感谢。如果
this.id,
类似于
$(this.id)因为我无法使此代码正常工作?非常感谢JSFIDLE或类似的工具。@mpdc--Nope,
这是指本机DOM元素。谢谢。除再次按下按钮时删除值外,我的操作正常。同样的代码。嗯…@mpdc——我做了一个编辑来修复它,行
currentValue=currentValue.replace(id+“,”,”)惊人,一定错过了那个。非常感谢。如果
this.id,
类似于
$(this.id)因为我无法使此代码正常工作?非常感谢JSFIDLE或类似的工具。@mpdc--Nope,
这是指本机DOM元素。谢谢。除再次按下按钮时删除值外,我的操作正常。同样的代码。嗯…@mpdc——我做了一个编辑来修复它,行
currentValue=currentValue.replace(id+“,”,”)惊人,一定错过了那个。非常感谢。如果
this.id,
类似于
$(this.id)因为我无法使此代码正常工作?非常感谢JSFIDLE或类似的工具。@mpdc--Nope,
这是指本机DOM元素。谢谢。除再次按下按钮时删除值外,我的操作正常。同样的代码。嗯…@mpdc——我做了一个编辑来修复它,行
currentValue=currentValue.replace(id+“,”,”)惊人,一定错过了那个。非常感谢。