jQuery事件将只运行一次
jQuery event will only run once
我正在使用table2excel插件,它将允许我将表格内容下载到excel文件中。它第一次运行得很好,但每次页面加载只允许我下载一次。
我尝试过将$("#export").click(function(){
更改为$("#export").on('click', function(){
,但没有成功。$('#export').点击位于文档就绪内
// click function on page
$("#export").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Excel Document Name"
});
});
// external js file that gets called
//table2excel.js
;(function ( $, window, document, undefined ) {
var pluginName = "table2excel",
defaults = {
exclude: ".noExl",
name: "Table2Excel"
};
// The actual plugin constructor
function Plugin ( element, options ) {
this.element = element;
// jQuery has an extend method which merges the contents of two or
// more objects, storing the result in the first object. The first object
// is generally empty as we don't want to alter the default options for
// future instances of the plugin
this.settings = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function () {
var e = this;
e.template = "<html xmlns:o='"urn:schemas-microsoft-com:office:office'" xmlns:x='"urn:schemas-microsoft-com:office:excel'" xmlns='"http://www.w3.org/TR/REC-html40'"><head><!--[if gte mso 9]><xml>";
e.template += "<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>";
e.template += "<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>";
e.tableRows = "";
// get contents of table except for exclude
$(e.element).find("tr").not(this.settings.exclude).each(function (i,o) {
e.tableRows += "<tr>" + $(o).html() + "</tr>";
});
this.tableToExcel(this.tableRows, this.settings.name);
},
tableToExcel: function (table, name) {
var e = this;
e.uri = "data:application/vnd.ms-excel;base64,";
e.base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)));
};
e.format = function (s, c) {
return s.replace(/{('w+)}/g, function (m, p) {
return c[p];
});
};
e.ctx = {
worksheet: name || "Worksheet",
table: table
};
window.location.href = e.uri + e.base64(e.format(e.template, e.ctx));
}
};
$.fn[ pluginName ] = function ( options ) {
this.each(function() {
if ( !$.data( this, "plugin_" + pluginName ) ) {
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
}
});
// chain jQuery functions
return this;
};
})( jQuery, window, document );
由于jquery插件中的代码块,它只执行一次:
$.fn[ pluginName ] = function ( options ) {
this.each(function() {
if ( !$.data( this, "plugin_" + pluginName ) ) {
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
}
});
// chain jQuery functions
return this;
};
CCD_ 3仅被初始化一次并且跳过随后的次数。
对您的代码进行了一些更改:
$("button").click(function(){
var $table = $('#table2excel');
$table.removeData(); //data is removed, previous when data existed was preventing initialization of table2excel
$table.table2excel({
exclude: ".noExl",
name: "Excel Document Name"
});
});
你可以在这里看到它的工作原理:http://jsfiddle.net/peterdotjs/bpLsrdy2/4/
其他然后
$("#export").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Excel Document Name"
});});
如果他们第二次点击它,你希望发生什么不同的事情?
你可能觉得只工作一次,因为它已经应用了更改,所以如果再次单击,它就没有其他操作要做,因为工作已经完成。
这是在这样一个假设下进行的,即脚本一直没有错误。如果有错误,您可以通过在Chrome中按f12进行检查。在屏幕的右上角,你应该看到是否有错误。如果它正在运行,然后在单击操作过程中失败,这可能是您发现问题的原因。
根据脚本(jquery.table2excel.js)的困扰不允许您多次运行它,所以为了解决这个问题,首先添加在你的文件末尾,在页面加载后,添加指向脚本的链接,现在你可以编写你的函数来调用导出到Excel
<script>
$(document).ready(function(){
$("#MyplaginEXL").attr("src","/TableToExcel/jquery.table2excel.js")
});
function PrintTable()
{
$("#IdTable").table2excel({
exclude: ".noExl",
filename: "FileName",
fileext: ".xls",
exclude_img: true,
exclude_links: true,
exclude_inputs: true,
preserveColors: true
});
}
相关文章:
- 使用每500ms运行一次的jquery函数是个好主意吗
- For循环在Jquery中只运行一次
- 谷歌图表在运行应用程序时只能显示一次
- 使函数只运行一次(Javascript)
- 更改路由后,运行一次js脚本
- 只运行一次提取的脚本
- 可能只在for循环内部运行一次代码块
- 在单击事件中,第一次单击时激发代码一次,其余代码在所有单击时运行
- 在Flask中使用.getJSON请求时,只运行一次命令
- 运行“;非“;单击另一个函数中的函数仅一次
- JavaScript只有在最后一次被调用时才运行函数
- 数据互绑定问题:转换器只运行一次,无法绑定元素的 ID
- 您可以在节点.js上一次运行多个应用程序吗?
- 如何一次运行一次循环javascript
- 从命令行一次运行大量摩卡测试的好方法是什么
- 如何让 JavaScript 一次运行一个字符串
- 无法在一次运行中删除多个数组元素
- 如何使jquery函数一次运行一个
- 在一次运行中分配并返回一个变量
- 一次运行2个动画