テキスト フィールドの値を使用して入力されている選択リストがあります。入力した値を選択リストに追加する追加ボタンと、入力した値を選択リストから削除する削除ボタンの 2 つのボタンもあります。jQueryを使用して次のことをしたいと思います:
- テキスト フィールドに入力された値が選択リストで使用できない場合は、追加ボタンを表示し、削除ボタンを非表示にします。
- テキスト フィールドに入力された値が選択リストでAVAILABLEの場合、追加ボタンを非表示にし、削除ボタンを表示します。
- 選択リストが空の場合、追加ボタンを表示し、削除ボタンを非表示にします。
ここに私が思いついたいくつかのコードがあります:
// Remove selected options
$('#removeButton').click(function() {
//$.map($('#addedchargeamtid :selected'), function(e) {
$.map($('#addedchargeamtid option'), function(e) {
var exp = $(e).text();
// Would like to have all the Option values in CVS format 0.00,1.99, etc...
// If removed this should also remove the value in the array
})
$('#removeButton').hide();
return !$('#addedchargeamtid :selected').remove();
});
// Add charge amount
$('#addedchargeamtid option:selected').focus(function() {
$('#removeButton').show();
});
最初の値を追加すると削除ボタンが表示されますが、値を削除するとボタンが元に戻りません。
アップデート:
さて、私はこれを編集しました。
$('#removeButton').click(function() {
$('#addedchargeamtid :selected').remove();
$.map($('#addedchargeamtid option'), function(e) {
var exp = $(e).text();
alert(exp); // Need this in CSV format but I think it displays the correct values
})
//if($("#addedchargeamtid option").length > 0) { <-- Didn't work
if($("#addedchargeamtid").length > 0) {
$('#removeButton').show();
} else {
$('#removeButton').hide();
}
});
SELECTに値がない場合でも、ボタンは非表示になりません。オプションも付けてみました。