Javascript 如何创建复选框id为';s

Javascript 如何创建复选框id为';s,javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,我对这件事束手无策 单击复选框时,我希望将复选框的ID添加到下面输入中以逗号分隔的字符串中。我有这个工作,但是,我不能做的是删除ID和它的逗号,如果它已经存在于输入字段中(选中和取消选中) 简单的形式 <form> <input class="iteminput" type="checkbox" value="1" id="1" name="<?php echo $title; ?>"> <input class="iteminput" type="c

我对这件事束手无策

单击复选框时,我希望将复选框的ID添加到下面输入中以逗号分隔的字符串中。我有这个工作,但是,我不能做的是删除ID和它的逗号,如果它已经存在于输入字段中(选中和取消选中)

简单的形式

<form>
<input class="iteminput" type="checkbox" value="1" id="1" name="<?php echo $title; ?>">
<input  class="iteminput" type="checkbox" value="2" id="2" name="<?php echo $title; ?>">
<input  class="iteminput" type="checkbox" value="3" id="3" name="<?php echo $title; ?>">

<!-- Note that this field may or may not have an existing string of ID's (1,2,3) from previous saves -->    
<input type="text" id="excludelist" value="<?php echo $saved-string; ?>">
</form>

jQuery(document).ready(function(){
    jQuery('.iteminput').on('click', function(){
        var id = jQuery(this).attr('ID');
        var string = jQuery('#excludelist').val();
        var newstring = string + id + ',';
        jQuery('#excludelist').val(newstring);
    })
})


您可以获取输入框的值,并使用该方法将ID字符串拆分为一个数组。此时,您可以检查正在查找的ID是否在该数组中。例如:

const id = 3;
const inputValue = $('input[type=text]').val();

// Split the IDs into an array by comma.
const currentIds = inputValue.split(',');

if (currentIds.indexOf(id) === -1) {
    // ID has not yet been added to the input box.
    // We can add it to the array here, and
    // update it later.
    currentIds.push(id);
} else {
    // ID is in the current list of IDs. We
    // can remove it like this:
    currentIds.splice(currentIds.indexOf(id), 1);
}

// Finally, we can reset the input string
// with the new values we set above.
$('input[type=text]').val(currentIds.join(','));
见:


您可以获取输入框的值,并使用该方法将ID字符串拆分为一个数组。此时,您可以检查正在查找的ID是否在该数组中。例如:

const id = 3;
const inputValue = $('input[type=text]').val();

// Split the IDs into an array by comma.
const currentIds = inputValue.split(',');

if (currentIds.indexOf(id) === -1) {
    // ID has not yet been added to the input box.
    // We can add it to the array here, and
    // update it later.
    currentIds.push(id);
} else {
    // ID is in the current list of IDs. We
    // can remove it like this:
    currentIds.splice(currentIds.indexOf(id), 1);
}

// Finally, we can reset the input string
// with the new values we set above.
$('input[type=text]').val(currentIds.join(','));
见:


为什么不重建它呢

var$iteminputs=$('.iteminput');
$iteminputs.on('change',function()){
var id=$.map($iteminputs.filter(':checked'),函数(元素){return element.id;});
$('#excludelist').val(id.join(',');
});

为什么不重建它呢

var$iteminputs=$('.iteminput');
$iteminputs.on('change',function()){
var id=$.map($iteminputs.filter(':checked'),函数(元素){return element.id;});
$('#excludelist').val(id.join(',');
});


“如果ID及其逗号已经存在于输入字段中,我不能做的是删除它。”在您尝试从
输入中删除字符串的问题中,是否可以包含
javascript
?如果可以在连接字符串之前检查字符串是否已存在于
.value
中,为什么要连接字符串?似乎最简单的方法就是在复选框状态更改时重建整个逗号选定列表。“如果输入字段中已经存在ID及其逗号,则我不能删除它“在您试图从
输入
.value
中删除字符串的问题中,是否可以包含
javascript
?如果可以在连接字符串之前检查字符串是否已存在于
.value
中,为什么要连接字符串?似乎最简单的方法就是在复选框状态更改时重新生成整个逗号选择列表。请展开代码并解释如何添加和删除该字符串输入字段中的ID?您可以根据需要从数组中添加/删除ID,然后使用JavaScript的
array.prototype.join()
方法重新创建字符串并将其设置为值。我会更新我的答案,给你一个更好的例子。更新了一个更完整的例子OK,看起来这是可行的,虽然-第一个id显示a,在它(,1或,2或,3)之前,我不确定这将通过我的PHP。我现在要做的就是将该字符串中的所有复选框标记为选中。您可以使用substring方法轻松删除前导逗号。您可以展开代码并解释如何在输入字段中添加和删除ID吗?您可以根据需要从数组中添加/删除ID,然后使用JavaScript的
array.prototype.join()
方法重新创建字符串并将其设置为值。我会更新我的答案,给你一个更好的例子。更新了一个更完整的例子OK,看起来这是可行的,虽然-第一个id显示a,在它(,1或,2或,3)之前,我不确定这将通过我的PHP。我现在要做的就是将该字符串中的所有复选框标记为选中。您可以使用substring方法轻松删除前导逗号