Hi i search and found some info but i cant just cant set a name for this function to call it again in other function to "reload" the data.
Im a noob so i try to wrap from var form to the en in a function myfunction() {} but did not work. any suggestions?
$(document).ready(function (){
var form = $('#formments');
var submit = $('#ments');
// send ajax request
$.ajax({
url: '<?PHP echo $this->make_base_url("user/ments_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
var item = $(data).hide().fadeIn(800);
$('.module').append(item);
var objDiv = document.getElementById("modulem");
objDiv.scrollTop = objDiv.scrollHeight;
// reset form and button
form.trigger('reset');
submit.val('Submit Comment').removeAttr('disabled');
},
error: function(e){
alert(e);
}
});
});
Hi i search and found some info but i cant just cant set a name for this function to call it again in other function to "reload" the data.
Im a noob so i try to wrap from var form to the en in a function myfunction() {} but did not work. any suggestions?
$(document).ready(function (){
var form = $('#formments');
var submit = $('#ments');
// send ajax request
$.ajax({
url: '<?PHP echo $this->make_base_url("user/ments_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./a/978731
var item = $(data).hide().fadeIn(800);
$('.module').append(item);
var objDiv = document.getElementById("modulem");
objDiv.scrollTop = objDiv.scrollHeight;
// reset form and button
form.trigger('reset');
submit.val('Submit Comment').removeAttr('disabled');
},
error: function(e){
alert(e);
}
});
});
Share
Improve this question
asked Nov 15, 2013 at 5:17
Moncho ChavezMoncho Chavez
6942 gold badges12 silver badges31 bronze badges
2 Answers
Reset to default 4It’s pretty straightforward to change a function literal into a function declaration. Just move it out, give it a name, and use that variable.
function ready() {
var form = $('#formments');
var submit = $('#ments');
// send Ajax request
$.ajax({
url: <?= json_encode($this->make_base_url("user/ments_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./a/978731
var item = $(data).hide().fadeIn(800);
$('.module').append(item);
var objDiv = document.getElementById("modulem");
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);
I also changed your PHP to use json_encode
, which handles any escaping for you, in the rare case it’s necessary. (There are some other minor changes, too.)
I would write it like this
(function($){
var init = function() {
// ...
};
$(init);
})(jQuery);
This anonymous closure keeps the init
function out of the global namespace.
Note: $(fn)
is a shortcut for $(document).ready(fn)
;
From the jQuery .ready docs
All three of the following syntaxes are equivalent:
$( document ).ready( handler )
$().ready( handler )
(this is not remended)$( handler )
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745465542a4628902.html
评论列表(0条)