如何在单击时禁用javascript以将表导出到excel?

如何在单击时禁用javascript以将表导出到excel?,javascript,jquery,html,excel,Javascript,Jquery,Html,Excel,我有一个包含db变量的表: <table id="itemall2" class="itemall" border="1"> <thead> <tr><th></th> <th><?= $lang['quantity']; ?></th> <th><?= $lang['article']; ?&g

我有一个包含db变量的表:

<table id="itemall2" class="itemall" border="1">
        <thead>
            <tr><th></th>
            <th><?= $lang['quantity']; ?></th>
            <th><?= $lang['article']; ?></th>
            <th><?= $lang['desc']; ?></th>
            <th><?= $lang['weight']; ?> (Kg)</th>
            <th><?= $lang['price']; ?> (Kč)</th>
            <th><?= $lang['cat_id']; ?></th>
            <th><?= $lang['cat_name']; ?></th></tr>
        </thead>
        <tbody>
        <?php foreach ($this->items as $value) : ?>
            <tr><td><input type="checkbox" name="checked[]" value="<?= $value['id_item']?>" onclick="enableName(this, 'quantity<?= $value['id_item']?>');"/></td>
                <td><input type="number" class="quantity<?= $value['id_item']?>" name="quantity[]" disabled></td>
                <td><?= $value['id_item']?></td>
                <td><?= $value[$en] ?></td>
                <td><?= $value['weight'] ?></td>
                <td><?= $value['price'] ?></td>
                <td><?= $value['code'] ?></td>
                <td><?= $value['name'] ?></td>
            </tr>
        <?php endforeach; ?>

            <input type="hidden" name="id_warehouse" type="text" value="<?= $this->id_warehouse ?>">
        </tbody>
    </table>

(千克)
(Kč)

您可以像这样修改tableToExcel函数

var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , 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><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table);
    // clone the table
    var modifiedTable = $('<table/>').html(table.innerHTML);
    // remove elements from the clone
    modifiedTable.find('.hidden').remove();
    // get the modified html
    var ctx = {worksheet: name || 'Worksheet', table: modifiedTable.html()}
    window.location.href = uri + base64(format(template, ctx))
  }
})();
var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称){
如果(!table.nodeType)table=document.getElementById(table);
//克隆表
var modifiedTable=$('').html(table.innerHTML);
//从克隆中删除元素
modifiedTable.find('.hidden').remove();
//获取修改后的html
var ctx={工作表:名称| |'工作表',表:modifiedTable.html()}
window.location.href=uri+base64(格式(模板,ctx))
}
})();
克隆和删除样本


这不会修改原始表

以下是jQuery的方法,它可以做与Crisim II Numenoreano相同的事情+多亏了他,我用他来测试下面的代码:

var clonedTable = $('#test').clone();

clonedTable.find('.hidden').remove();

我认为您可以克隆合并的表,然后从克隆的表中删除隐藏的元素。对于Excel导出,我将使用该克隆表。更多信息。我没有测试过它,但它可以工作。我可以删除行跨度并显示隐藏元素,但我不擅长javascript,所以我只对我拥有的表进行了测试。但我最终没有合并原来的表,这不是我想要的。excel看起来不错,但我要么需要以某种方式重新合并它,要么像你说的那样在某个副本上进行合并,但我不知道如何进行合并。
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , 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><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table);
    // clone the table
    var modifiedTable = $('<table/>').html(table.innerHTML);
    // remove elements from the clone
    modifiedTable.find('.hidden').remove();
    // get the modified html
    var ctx = {worksheet: name || 'Worksheet', table: modifiedTable.html()}
    window.location.href = uri + base64(format(template, ctx))
  }
})();
var clonedTable = $('#test').clone();

clonedTable.find('.hidden').remove();