gpt4 book ai didi

php - 使用 jQuery 禁用 append 下拉列表中的选项

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:49:59 26 4
gpt4 key购买 nike

我正在使用 Yii2 php 框架,这是我显示第一个下拉列表的方式:

<?php 
$deductions = PayrollItems::find()->where(['store_id' => $session['store_id']])->orwhere(['store_id' => null])->where(['payroll_type' => 'Deduction'])->all();
$deductionslistData = ArrayHelper::map($deductions,'payroll_items_id', 'payroll_name');
echo $form->field($model2, 'deduction_item_id')->widget(Select2::classname(), [
'data' => $deductionslistData,
'options' => ['placeholder' => 'Select a Deduction ', 'id' => 'deduction_item'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>

这是 append 一个新的下拉列表的脚本函数:

function addDeductionItem(){   
var count = $('#count2').val();
if($('#deduction_item').val()=="" || $('#deduction_item_' + count).val()==""){
alert("Please fill in the earning item field first!");
}
else{
count++ ;
$('#count2').val(count);

$.ajax({
url: 'index.php?r=payslip/deductions',
dataType: 'json',
method: 'GET',
data: {},
success: function (data, textStatus, jqXHR) {
$("#deduction_item_" + count).append($("<option></option>").html(""));
//$("#deduction_item_" + count).append($("<option></option>").val('add item').html("Add New Item"));
for(i=0; i<data.length;i++)
{
$("#deduction_item_" + count).append($("<option></option>").val(data[i][1]).html(data[i][0]));
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('An error occured!');
alert('Error in ajax request');
}
});

var deduction = '<tr id="row_' + count +'"><td><select class="form-control input-md" placeholder="Select a Deduction " style="width: 100%; cursor: pointer;" data-krajee-select2="select2_6789bf5d" tabindex="-1" id="deduction_item_' + count + '" name="deduction_item_' + count + '"></select></td><td><input class="form-control" type="text" id="deduction_unit_' + count + '" name="deduction_unit_' + count + '"></td><td><input class="form-control" type="text" id="deduction_amount_' + count + '" name="deduction_amount_' + count + '" onchange="_getTotalDeduction(id)"></td>';

deduction += '<td><a style="cursor:pointer" class="ibtnDel"><i class="fa fa-times">&nbsp;&nbsp;</a></td></tr>';

$("#deduction_items").append(deduction);

$("#deduction_items").on('click','.ibtnDel',function(){
$(this).closest('tr').remove();
});

$('#row_' + count + ' select').select2();
}
}

我有一个下拉列表,例如它有 3 个选项,然后我选择选项 01。现在,当我 append 一个新的下拉列表时(在我的例子中单击“添加扣除项”按钮) , append 下拉列表中的选项 01 将被禁用。

整个想法是,当我在第一个下拉列表中选择一个选项时,所选选项现在应该在 append 的下拉列表中被禁用,依此类推。

我在网上搜索过这个问题。我找到了一些,但它们对我不起作用,因为我的问题与我发现的问题不太相似。

我找到并尝试了这个 fiddle :http://jsfiddle.net/rajan222000/yww8apn9/1/但仍然不适合我。我注意到它有静态数量的下拉列表。我试过 var deductionID = 'deduction_item_' + count + ''; 让它动态化,但我不知道为什么它不起作用。

我真的需要你的帮助。

最佳答案

如果我理解正确,那么您需要获取已生成下拉列表的所有选定值,并在新创建的下拉列表中禁用它。

如果不进行任何代码重构,您需要启动一个简单的 for 循环并禁用新下拉列表中的所有选定值。

将此添加到 Ajax 调用中成功函数的末尾应该可以解决问题:

for(var c=count-1; c>=0; c--) {
var sel = $("#deduction_item_" + c).val();
$("#deduction_item_" + count).find("option[value'"+sel+"']").prop('disabled', true);
}

关于php - 使用 jQuery 禁用 append 下拉列表中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29504602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com