Javascript 0]=$(示例tr:last td:nth child(12)).text(); 单元格[1]=$(“#示例tr:last td:nth child(13)”).text(); $('#示例tr:last').find('td:nth child(12)').html(accounting.formatMoney(cell[0]); $('#示例tr:last').find('td:nth child(13)').html(accounting.formatMoney(cell[1]); $('#示例tr:last').find('td:last').hide(); }//隐藏正在显示的额外td }); //Datatable()的结尾 }); //调用结束。完成函数 $('theTableDiv')。向下滑动(); }//运行结束()函数

Javascript 0]=$(示例tr:last td:nth child(12)).text(); 单元格[1]=$(“#示例tr:last td:nth child(13)”).text(); $('#示例tr:last').find('td:nth child(12)').html(accounting.formatMoney(cell[0]); $('#示例tr:last').find('td:nth child(13)').html(accounting.formatMoney(cell[1]); $('#示例tr:last').find('td:last').hide(); }//隐藏正在显示的额外td }); //Datatable()的结尾 }); //调用结束。完成函数 $('theTableDiv')。向下滑动(); }//运行结束()函数,javascript,jquery,ajax,sharepoint,spservices,Javascript,Jquery,Ajax,Sharepoint,Spservices,我不是程序员,我只是在努力学习。我将感谢任何帮助。提前感谢我猜您正在替换页面中按钮所在的部分。(您确实需要更整洁地格式化您的代码,因此……请使用JSFiddle.net及其TidyUp按钮) 如果是这种情况,则需要使用委托事件处理程序: 这将侦听所需节点的静态祖先,然后在事件发生时运行选择器,然后将函数应用于导致事件的任何匹配元素 如果您没有方便的祖先,document是后备父级。不要将“body”用于委托事件,因为它具有奇怪的行为。浏览器控制台中是否有错误?假设在点击按钮之间没有页面刷新,那么

我不是程序员,我只是在努力学习。我将感谢任何帮助。提前感谢

我猜您正在替换页面中按钮所在的部分。(您确实需要更整洁地格式化您的代码,因此……请使用JSFiddle.net及其TidyUp按钮)

如果是这种情况,则需要使用委托事件处理程序:

这将侦听所需节点的静态祖先,然后在事件发生时运行选择器,然后将函数应用于导致事件的任何匹配元素


如果您没有方便的祖先,
document
是后备父级。不要将“body”用于委托事件,因为它具有奇怪的行为。

浏览器控制台中是否有错误?假设在点击按钮之间没有页面刷新,那么问题很可能是您试图针对已经初始化的元素(#示例)初始化数据表。如果是这种情况,您将在控制台中看到一个确认错误。如果DataTables已经初始化,正确的方法是使用它们的API更新现有的表,然后通过.draw()刷新它。谢谢Stevangelista!我一点错误都没有。我想你是对的,似乎是数据表让我头疼。我试过draw()、drawCallback()等,但没有成功。我离实现我的想法越来越近了。你为什么要使用jQueryAjax?所以你不必重新初始化。谢谢你的代码建议和TidyUp工具(顺便说一句,非常有用)。但是你的解决方案不起作用。正如Stevangelista所建议的,问题在于DataTables插件。不知何故,我需要将draw()或drawCallback()函数绑定到我的按钮上,但到目前为止还没有成功完成。真丢脸。这回答了90%的问题只适用于一次:)请应用TidyUp,因为它是很难阅读的代码,因为它是格式化的。
$(document).ready(function () {
var theYear; // Selected Year
var theRO; // Selected RO
//Fills the Dropdown lists (Year and RO)
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "{ListID}",
    CAMLViewFields: "<ViewFields><FieldRef Name='Fiscal_x0020_Year' /><FieldRef    Name='Regional_x0020_Office' /></ViewFields>",
    completefunc: function (xData, Status) {
        //Add Select Value option
        $("#dropdown").prepend($('<option>', {
            value: '',
            text: 'Select Fiscal Year'
        }));
        $("#dropdownRO").prepend($('<option>', {
            value: '',
            text: 'Select Regional Office'
        }));
        //Fetching Data from SharePoint    
        $(xData.responseXML).SPFilterNode("z:row").each(function () {
            var dropDown = "<option value='" + $(this).attr("ows_Fiscal_x0020_Year") + "'>" + $(this).attr("ows_Fiscal_x0020_Year") + "</option>";
            var dropDownRO = "<option value='" + $(this).attr("ows_Regional_x0020_Office") + "'>" + $(this).attr("ows_Regional_x0020_Office") + "</option>";
            $("#dropdown").append(dropDown);
            $("#dropdownRO").append(dropDownRO);
            /////////////Deletes duplicates from dropdown list////////////////
            var usedNames = {};
            $("#dropdown > option, #dropdownRO > option").each(function () {
                if (usedNames[this.text]) {
                    $(this).remove();
                } else {
                    usedNames[this.text] = this.value;
                }
            });
            ////Deletes repeated rows from table
            var seen = {};
            $('#myTable tr, #tasksUL li, .dropdown-menu li').each(function () {
                var txt = $(this).text();
                if (seen[txt]) $(this).remove();
                else seen[txt] = true;
            });
        });
    } //end of completeFunc
}); //end of SPServices
$('.myButton').on('click', function () {
    run()
});
}); //End jQuery Function
        function run() {

theYear = $('#dropdown option:selected').text(); // Selected Year
theRO = $('#dropdownRO option:selected').text(); // Selected RO
var call = $.ajax({
    url: "https://blah-blah-blah/_api/web/lists/getByTitle('Consolidated%20LC%20Report')/items()?$filter=Fiscal_x0020_Year%20eq%20'" + theYear + "' and Regional_x0020_Office eq '" + theRO + "'&$orderby=Id&$select=Id,Title,Fiscal_x0020_Year,Notices_x0020_Received,Declined_x0020_Participation,Selected_x0020_Field_x0020_Revie,Selected_x0020_File_x0020_Review,Pending,Pending_x0020_Previous_x0020_Yea,Controversial,GFP_x0020_Reviews,NAD_x0020_Appeals,Mediation_x0020_Cases,Monthly_x0020_Cost_x0020_Savings,Monthly_x0020_Expenditure,Regional_x0020_Office,Month_Number", //Works, filters added
    type: "GET",
    cache: false,
    dataType: "json",
    headers: {
        Accept: "application/json;odata=verbose",
    }
}); //End of ajax function///
call.done(function (data, textStatus, jqXHR) {
    var oTable = $('#example').dataTable({
        "aLengthMenu": [
            [25, 50, 100, 200, -1],
            [25, 50, 100, 200, "All"]
        ],
            "iDisplayLength": -1, //Number of rows by default. -1 means All Records
        "sPaginationType": "full_numbers",
            "aaData": data.d.results,
            "bJQueryUI": false,
            "bProcessing": true,
            "aoColumns": [{
            "mData": "Id",
            "bVisible": false
        }, //Invisible column
        {
            "mData": "Title"
        }, {
            "mData": "Notices_x0020_Received"
        }, {
            "mData": "Declined_x0020_Participation"
        }, {
            "mData": "Selected_x0020_Field_x0020_Revie"
        }, {
            "mData": "Selected_x0020_File_x0020_Review"
        }, {
            "mData": "Pending"
        }, {
            "mData": "Pending_x0020_Previous_x0020_Yea"
        }, {
            "mData": "Controversial"
        }, {
            "mData": "GFP_x0020_Reviews"
        }, {
            "mData": "NAD_x0020_Appeals"
        }, {
            "mData": "Mediation_x0020_Cases"
        }, {
            "mData": "Monthly_x0020_Cost_x0020_Savings",
            "fnRender": function (obj, val) {
                return accounting.formatMoney(val);
            }
        }, {
            "mData": "Monthly_x0020_Expenditure",
            "fnRender": function (obj, val) {
                return accounting.formatMoney(val);
            }
        }],
            "bDeferRender": true,
        "bRetrieve": true,
        "bInfo": true,
        "bAutoWidth": true,
        "bDestroy": true,
            "sDom": 'T&;"clear"&;frtip',
            "oTableTools": {
            "aButtons": ["xls"],
                "sSwfPath": "../../Style Library/js/datatables/TableTools/media/swf/copy_csv_xls_pdf.swf",
        },
            "sSearch": "Filter",
            "fnDrawCallback": function () {
            //Add totals row
            var Columns = $("#example > tbody").find("> tr:first > td").length;
            $('#example tr:last').after('<tr><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td><td  class="total"></td></tr>');
            //Formating the Total row number to no decimals
            $("#example tr:last td:not(:first,:last)").text(function (i) {
                var t = 0;
                $(this).parent().prevAll().find("td:nth-child(" + (i + 2) + ")").each(function () {
                    t += parseFloat($(this).text().replace(/[\$,]/g, '') * 1);
                });
                return parseInt(t * 100, 10) / 100;
            });
            //Format the monthly expenditure and savings to currency formatFormating the currency
            var cell = new Array();
            cell[0] = $('#example tr:last td:nth-child(12)').text();
            cell[1] = $('#example tr:last td:nth-child(13)').text();
            $('#example tr:last').find('td:nth-child(12)').html(accounting.formatMoney(cell[0]));
            $('#example tr:last').find('td:nth-child(13)').html(accounting.formatMoney(cell[1]));
            $('#example tr:last').find('td:last').hide();
        } //hides extra td that was showing
    }); //End of Datatable()
}); //End of call.done function
$('#theTableDiv').slideDown();
} //end of run() function
$(document).on('click', '.myButton', function () {
    run()
});