2

我为这个函数加载了一个外部页面。来自 DIV id 的页面 URL 抓取。

$(".contentArea").load($('.contentArea').attr('id'), function(){

加载的页面,有一个数据表,所以我将它添加到回调中。

$('.datatable').dataTable( {

在数据表内部,我得到了一个编辑器按钮,所以我使用数据表回调函数调用编辑器而不刷新页面:

"fnDrawCallback": function(){

    $(".contentEditor").click(function() {

        $(".contentArea").load($('.contentEditor').attr('id'), function(){

在这种情况下,内容编辑器的加载方式与我加载包含数据表的页面的方式相同。(在按钮 ID 上传递的页面 URL)。

我现在卡住了。在这个编辑器上,我需要提交表单,我希望它使用 jquery load 提交,这样页面就不会刷新,提交表单后,我想将冲浪者发送回数据表页面(第一次加载时的页面)页面已加载)。我将执行更新编辑内容所需的操作。有什么帮助吗?谢谢。

  • 我使用数据表服务器端 ajax 加载。这就是我使用回调的原因。

        $(".contentArea").load($('.contentArea').attr('id'), function(){
            $('.datatable').dataTable( {
                "bJQueryUI": true,
                "sScrollX": "",
                "bSortClasses": false,
                "aaSorting": [[0,'asc']],
                "bAutoWidth": true,
                "bInfo": true,
                "sScrollY": "100%", 
                "sScrollX": "100%",
                "bScrollCollapse": true,
                "sPaginationType": "full_numbers",
                "bRetrieve": true,
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": $('.datatable').attr('id'),
                "fnDrawCallback": function(){
                    $(".contentEditor").click(function() {
                        $(".contentArea").load($('.contentEditor').attr('id'), function(){
                            $( "select, input:checkbox, input:radio, input:file").uniform(),
                            $( ".datepicker" ).datepicker({dateFormat: 'yy-mm-dd' }),
                            $("#validation").validationEngine(),
                            $('input[title]').tipsy(),
                            $('textarea.tinymce').tinymce({
                                 // Location of TinyMCE script
                                script_url : '../scripts/tinyeditor/tiny_mce.js',
    
                                // General options
                                theme : "advanced",
                                plugins : "table,advhr,advimage,advlink,inlinepopups,preview,media,paste,fullscreen,visualchars,xhtmlxtras",
    
                                // Theme options
                                theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,forecolor,backcolor",
                                theme_advanced_buttons2 : "formatselect,fontselect,fontsizeselect,|,removeformat,|,hr,|,undo,redo,|,sub,sup,|,charmap,|,cite",
                                theme_advanced_buttons3 : "tablecontrols,|,link,unlink,anchor,|,image,preview,media,|,cleanup,code,fullscreen",
                                theme_advanced_toolbar_location : "top",
                                theme_advanced_toolbar_align : "left",
                                theme_advanced_statusbar_location : "bottom",
                                theme_advanced_resizing : true
                            });
                        });
                    });
                }});
            });  
    
4

1 回答 1

1

要提交表单,请$.post()与 一起使用$(form).serialize()。然后,在成功处理程序中$.post().load()再次使用。

$.post(url, $("#myForm").serialize(), function(data) {
    $(".contentArea").load(...);
});

或者,如果提交表单返回的内容是您要在其中显示的 html .contentArea,您可以.load()通过使用返回的 html in来节省额外的调用.contentArea

$.post(url, $("#myForm").serialize(), function(data) {
    $(".contentArea").html(data);
});

编辑: 创建函数来处理不同的任务。顺便说一句,不要id用于存储 url。创建一个自定义属性...也许contentUrl

var contentArea = $(".contentArea");
function loadContent(url, success) {
    contentArea.load(url, success);
}
function loadDataTable() {
    loadContent(contentArea.attr("contentUrl"), initDataTable);
}
function initDataTable() {
    $(".datatable").dataTable({
        ...,
        fnDrawCallback: bindContentEditor
    });
}
function bindContentEditor() {
    $(".contentEditor").click(contentEditorClick);
}
function contentEditorClick(e) {
    loadContent($(".contentEditor").attr("contentUrl"), initContentEditor);
}
function initContentEditor() {
    ...
    $(".submitBtn").click(postContentEditor);
}
function postContentEditor() {
    $.post("/postUrl", $(".contentArea form").serialize(), loadDataTable);
}
loadDataTable();

我把它分解成可能太多的单独函数,但重点是不要过度使用匿名函数,尤其是当你想重用功能时。

于 2011-09-23T23:40:47.107 回答