我正在学习jQuery。我想添加一个功能,所以当用户单击td
具有指定类名的 a 时,我想将其更改为一个select
选项。使用该选定值,我想更新我的数据库。
直到这里还好。我可以将 更改td
为input
但是,我无法将更改事件中的事件添加到该替换元素。
这是我所拥有的:
(function($) {
var _defaults = {
some : "defaults",
};
$.fn.changetoi = function(options) {
var _opts = $.extend({}, _defaults, options);
$(this).on('click', function() {
var replaced = $('<select name="status" class="changer"><option>Test</option></select>');
$(this).replaceWith(replaced);
});
$('.changer').on('change', function() { //here I'm try to access the changed element
var value = $(".changer option:selected").text();
//post the selected value
$.post("test.php", {
"value" : value
}, function(data) {
//whatever and
$(this).replaceWith(value);
});
});
};
$('.change').changetoi();
})(jQuery);