chosen ajax获取数据,Jquery Chosen插件 - 由Ajax动态填充列表

小智..

5

选择的答案已经过时,熔化/ ajax选择的插件也是如此.

使用Select2插件有很多错误,我无法解决它.

这是我对这个问题的回答.

//setup before functions

var typingTimer; //timer identifier

var doneTypingInterval = 2000; //time in ms (2 seconds)

var selectID = 'YourSelectId'; //Hold select id

var selectData = []; // data for unique id array

//on keyup, start the countdown

$('#' + selectID + '_chosen .chosen-choices input').keyup(function(){

// Change No Result Match text to Searching.

$('#' + selectID + '_chosen .no-results').html('Searching = "'+ $('#' + selectID + '_chosen .chosen-choices input').val() + '"');

clearTimeout(typingTimer); //Refresh Timer on keyup

if ($('#' + selectID + '_chosen .chosen-choices input').val()) {

typingTimer = setTimeout(doneTyping, doneTypingInterval); //Set timer back if got value on input

}

});

//user is "finished typing," do something

function doneTyping () {

var inputData = $('#' + selectID + '_chosen .chosen-choices input').val(); //get input data

$.ajax({

url: "YourUrl",

data: {data: inputData},

type:'POST',

dataType: "json",

beforeSend: function(){

// Change No Result Match to Getting Data beforesend

$('#' + selectID + '_chosen .no-results').html('Getting Data = "'+$('#' + selectID + '_chosen .chosen-choices input').val()+'"');

},

success: function( data ) {

// iterate data before append

$.map( data, function( item ) {

// matching data eg: by id or something unique; if data match: not append - else: append

// This will prevent from select the same thing twice.

if($.inArray(item.attr_hash,selectData) == -1){

// if not match then append in select

$('#' + selectID ).append('' + item.data + '');

}

});

// Update chosen again after append

$('#' + selectID ).trigger("chosen:updated");

}

});

}

// Chosen event listen on input change eg: after select data / deselect this function will be trigger

$('#' + selectID ).on('change', function() {

// get select jquery object

var domArray = $('#' + selectID ).find('option:selected');

// empty array data

selectData = [];

for (var i = 0, length = domArray.length; i < length; i++ ){

// Push unique data to array (for matching purpose)

selectData.push( $(domArray[i]).data('id') );

}

// Replace select to only selected option

$('#' + selectID ).html(domArray);

// Update chosen again after replace selected

$('#' + selectID ).trigger("chosen:updated");

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值