我正在处理可拖动的项目。我试图存储可拖动div的最终位置,一旦它被拖动停止。当我有以下JavaScript代码时它工作正常。Uncaught TypeError:无法读取未定义的属性'位置'
function make_draggable(elements)
{
/* Elements is a jquery object: */
elements.draggable({
containment: 'parent',
start: function (e, ui) {
ui.helper.css('z-index', ++zIndex);
},
stop: function (e, ui) {
/* Sending the z-index and positon of the note to update_position.php via AJAX GET: */
$.get('ajax/update_position.php', {
x: ui.position.left,
y: ui.position.top,
z: zIndex,
id: parseInt(ui.helper.find('span.data').html())
});
}
});
}
我不希望每次用户停止拖动div时都要更新数据库。所以我想添加一个确认按钮(当用户停止拖动时出现)来进行AJAX调用。所以,我想下面的代码
var $button = $("#button");
function make_draggable(elements) {
/* Elements is a jquery object: */
elements.draggable({
containment: 'parent',
start: function (e, ui) {
ui.helper.css('z-index', ++zIndex);
$("#button").animate({
opacity: 0
}, 1000);
},
stop: function (e, ui) {
/* Sending the z-index and positon of the note to update_position.php via AJAX GET: */
$('#chatAudio')[0].play();
$("#button").animate({
opacity: 1
}, 1000);
/* $.get('ajax/update_position.php',{x : ui.position.left,y: ui.position.top,z: zIndex,id:parseInt(ui.helper.find('span.data').html())});*/
} /*stop is ending */
}); /* element.draggable is ending */
}
function myFunction() {
alert(1);
/* Sending the z-index and positon of the note to update_position.php via AJAX GET: */
$.get('ajax/update_position.php', {
x: ui.position.left,
y: ui.position.top,
z: zIndex,
id: parseInt(ui.helper.find('span.data').html())
});
}
但是当我点击确认按钮,我看到我的JavaScript控制台上的Uncaught ReferenceError: ui is not defined
错误。
我以为没有定义ui
。所以我说(e,ui)
如下图所示
function myFunction(e,ui) {
alert(1);
/* Sending the z-index and positon of the note to update_position.php via AJAX GET: */
$.get('ajax/update_position.php', {
x: ui.position.left,
y: ui.position.top,
z: zIndex,
id: parseInt(ui.helper.find('span.data').html())
});
}
但现在我越来越Uncaught TypeError: Cannot read property 'position' of undefined
这里是我的website
你究竟在哪里调用函数?如果你在'stop:'这样调用它''myFunction(e,ui)'它应该按照预期工作 – Spokey
你应该console.log(ui)并且看看它是否有值.. –
@JFit它说“ ui没有被定义“ –