Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created January 15, 2010 05:17
Show Gist options
  • Save yuroyoro/277829 to your computer and use it in GitHub Desktop.
Save yuroyoro/277829 to your computer and use it in GitHub Desktop.
jQuery(function ($) {
$.fn.ajaxDynamicSelect = function (target ,options) {
var opt = $.extend({
url:'', // JSONを返すURLを設定
paramName:'selectedValue', // JSONを取得する際にサーバに送るパラメータ名
labelProperty:'text', // JSONの中でSelectのlabelに該当するプロパティ名
valueProperty:'value' // JSONの中でSelectのvalueに該当するプロパティ名
},options);
$(this).change( function(e){
var param = {};
param[opt.paramName] = $(this).val();
$.getJSON( opt.url,param ,
function(json){
$(target).empty();
for(var i = 0 ;i < json.length; i++){
$(target).append(
$(document.createElement('option'))
.attr({value:json[i][opt.valueProperty] })
.text(json[i][opt.labelProperty])
);
}
}
);
});
};});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment