jq 下拉列表选中事件,jQuery下拉选择事件

本文讨论了在网页表单中处理多个下拉框交互的问题。作者遇到的问题是,当用户在第一个下拉框中做出选择时,通过AJAX更新第二个下拉框的选项,并希望在用户完成选择后才移除禁用属性。最初使用的是.change()事件,但发现这会在选项变化时立即触发,导致其他下拉框提前启用。为解决此问题,作者改用'select2-blur'事件,并在AJAX成功回调中移除禁用属性,从而确保在正确的时间启用下一个下拉框。
摘要由CSDN通过智能技术生成

Is there a specific event for when a user selects an item in a dropdown box? I am creating a fairly large form that uses multiple dropdown boxes. On load all but the first are disabled, as the user selects the first option the options in the second box change using AJAX and I want to remove the attribute disabled.

$('#site_id').on('change', function(event) {

$('#access_type_id').removeAttr('disabled');

});

The trouble I am having is that .change() seems to be fired when the options in a dropdown change making following dropdown boxes enabled before I actually want them to be. Is there an alternative event I can use in place of change to only "enable" a form field once the user has made their selection in the previous dropdown?

UPDATE

I managed to solve this by not using the change event at all and placing my removeAttr in the success section of my AJAX call. An example is below. This allowed me to make the "next" dropdown enabled at the time as populating its options.

$('#access_speed_id').on('select2-blur', function(event){

$.ajax({

async: true,

data: $('#access_speed_id').serialize(),

dataType: 'html',

success: function(data, textStatus) {

$('#cdr_id').removeAttr('disabled');

$('#cdr_id').html(data);

},

type: 'post',

url: '/services/specificCdrs'

});

});

解决方案

Try Below code:

Volvo

Saab

Mercedes

Audi

Jquery

$(document).ready(function(){

$("#drop2").attr("disabled", true);

$("#drop1").change(function(){

$("#drop2").attr("disabled", false);

//Put your ajax code here and bind like below code

$("#drop2").append(new Option("option text", "value"));

$("#drop2").append(new Option("xxx","1"));

});

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值