2016-12-22 5 views
0

私の知恵はこれで終わりです。チェックボックスIDの動的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); 
    }) 
}) 
+0

_ _あなたは含めることができます「は、何私が行うことができないことがすでに入力フィールドに存在する場合はIDとそのコンマを削除している」 'あなたは、文字列を削除しようとした質問でjavascript' 'input'' .value'から?文字列を連結する前に文字列が '.value'内にすでに存在するかどうかを確認できるのはなぜですか? – guest271314

+0

これは、チェックボックスの状態が変わるたびにコンマ選択リスト全体を再構築するのが最も簡単なアプローチのようです。 – Taplar

答えて

1

あなたは、入力ボックスの値を取得し、配列にIDの文字列を分割するsplitメソッドを使用することができます。その時点で、探している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(',')); 

参照:

String.prototype.split()

Array.prototype.indexOf()

Array.prototype.push()

Array.prototype.splice()

Array.prototype.join()

+0

コードを拡張して、入力フィールドにIDを追加したり削除したりする方法を説明してください。 –

+0

必要に応じて配列からIDを追加/削除し、JavaScriptの 'Array.prototype.join()'メソッドを使用して文字列を再作成し、値として設定することができます。私はあなたに良い例を与えるために私の答えを更新します。 – samrap

+0

より完全な例で更新されました – samrap

1

なぜそれを再構築しないのですか?

var $iteminputs = $('.iteminput'); 
 

 
$iteminputs.on('change', function(){ 
 
    var ids = $.map($iteminputs.filter(':checked'), function(element){ return element.id; }); 
 
    $('#excludelist').val(ids.join(',')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input class="iteminput" type="checkbox" value="1" id="1" name="title1"> 
 
<input class="iteminput" type="checkbox" value="2" id="2" name="title2" checked> 
 
<input class="iteminput" type="checkbox" value="3" id="3" name="title3" checked> 
 

 
<!-- 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="2,3">

関連する問題