林在我的智慧結束與這一個。如何創建一個動態jQuery字符串的複選框ID的
當我點擊一個複選框時,我想在下面的輸入中將複選框的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和逗號,如果它已經在輸入欄中存在」 _您可以包括'javascript'在問,你試圖消除串從'輸入''.value'?爲什麼要連接字符串,如果您可以在連接字符串之前檢查字符串是否已存在於.value中? – guest271314
似乎最簡單的方法就是在複選框狀態發生變化時重建整個逗號選擇列表。 – Taplar