gpt4 book ai didi

javascript - 设置 $document 就绪函数的名称

转载 作者:行者123 更新时间:2023-11-28 13:38:00 25 4
gpt4 key购买 nike

您好,我搜索并找到了一些信息,但我不能只是为此函数设置一个名称,以便在其他函数中再次调用它来“重新加载”数据。

我是个菜鸟,所以我尝试在函数 myfunction() {} 中从 var 形式包装到 en,但没有成功。有什么建议吗?

$(document).ready(function (){
var form = $('#formcomments');
var submit = $('#comments');

// send ajax request
$.ajax({
url: '<?PHP echo $this->make_base_url("user/comments_all/".$resdata['id']."/");?>',
type: 'POST',
cache: false,
data: form.serialize(), //form serizlize data
beforeSend: function(){
// change submit button value text and disabled it
submit.val('Enviando...').attr('disabled', 'disabled');
},
success: function(data){
// Append with fadeIn see http://stackoverflow.com/a/978731
var item = $(data).hide().fadeIn(800);
$('.module').append(item);
var objDiv = document.getElementById("modulecomm");
objDiv.scrollTop = objDiv.scrollHeight;
// reset form and button
form.trigger('reset');
submit.val('Submit Comment').removeAttr('disabled');

},
error: function(e){
alert(e);
}
});
});

最佳答案

将函数文字更改为函数声明非常简单。只需将其移出,为其命名,然后使用该变量即可。

function ready() {
var form = $('#formcomments');
var submit = $('#comments');

// send Ajax request
$.ajax({
url: <?= json_encode($this->make_base_url("user/comments_all/{$resdata['id']}/")) ?>,
type: 'POST',
data: form.serialize(), //form serizlize data
beforeSend: function() {
// change submit button value text and disabled it
submit.val('Enviando...').prop('disabled', true);
},
success: function(data) {
// Append with fadeIn see http://stackoverflow.com/a/978731
var item = $(data).hide().fadeIn(800);
$('.module').append(item);
var objDiv = document.getElementById("modulecomm");
objDiv.scrollTop = objDiv.scrollHeight;
// reset form and button
form.trigger('reset');
submit.val('Submit Comment').prop('disabled', false);
},
error: function(e) {
alert(e);
}
});
}

$(document).ready(ready);

我还更改了您的 PHP 以使用 json_encode,它可以为您处理任何转义,在极少数情况下这是必要的。 (还有其他一些细微的变化。)

关于javascript - 设置 $document 就绪函数的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19994063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com