简体   繁体   中英

Is it possible to append code to the CakePHP-generated document.ready block?

I currently use the CakePHP JsHelper as follows and I would like to run additional JavaScript code when the document is ready:

    echo $this->Js->get(':submit')->event('click', "$(':submit').attr('disabled','disabled');
        $(':submit').val(\"Saving...\");
        ",
             array('stop' => false));

The above code is in my default.ctp and is inserted into every single page in this website application. I would like to add the additional code inside 1 page. The code would append a button with certain properties and events dangling from it. Is it possible to do so?

I have already tried having 2 documentReady functions (1 generated by CakePHP and the other inside a *.js file) to no avail.

Here are the current contents of the 2nd .js file:

//source: http://marcgrabanski.com/articles/cakephp-ajax-quick-save-jquery
$(document).ready(function() {
$('<input type="button" value="Insta-Save"/>')
.click(function(){ 
$(this).parents("form:first").ajaxSubmit({
success: function(responseText, responseCode) {
$('#ajax-save-message').hide().html(responseText).fadeIn();
setTimeout(function(){
$('#ajax-save-message').fadeOut();
}, 5000);
}
});
return false;
})
.appendTo('form div.submit');
});

I also tried using a code block but CakePHP insisted on stuffing the code in at the very top of the page and so naturally, IE complained.

Success!

Simply append to the CakePHP buffer as follows:

echo $this->Js->buffer('alert("Hello world!");');

And, just as the commenter pointed out above, everything works just fine with the 2 resulting documentReady functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM