php中禁用下拉框,php – 使用jQuery禁用附加下拉列表中的选项

本文探讨了如何在Yii2 PHP框架中,通过Ajax实现下拉列表联动,当第一个下拉列表选择项改变时,动态禁用附加下拉列表中对应选项。作者分享了使用Select2插件和自定义脚本函数来管理多个下拉列表的复杂情况。
摘要由CSDN通过智能技术生成

我正在使用Yii2 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

],

]);

?>

以下是用于附加新下拉列表的脚本函数:

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($("").html(""));

//$("#deduction_item_" + count).append($("").val('add item').html("Add New Item"));

for(i=0; i

{

$("#deduction_item_" + count).append($("").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 = '

';

deduction += '

  ';

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

$("#deduction_items").on('click','.ibtnDel',function(){

$(this).closest('tr').remove();

});

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

}

}

我有一个下拉列表,例如它有3个选项,然后我选择选项01.现在,当我添加一个新的下拉列表时(通过点击我的案例中的“添加扣除项目”按钮),选项01在附加的下拉列表中将被禁用.

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

我通过互联网搜索了这个问题.我发现了一些,但他们只是不适合我,因为我的问题与我发现的问题不太相似.

我发现并尝试了这个小提琴:http://jsfiddle.net/rajan222000/yww8apn9/1/

但仍然不适合我.我注意到它有静态的下拉列表数.我试过var deductionID =’deduction_item_’count”;使它动态,但我不知道为什么它不起作用.

我真的需要你的帮助.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值