js中eval() 方法的使用以及一…

本文详细介绍了JavaScript中的eval函数,包括其使用限制、参数处理方式、如何返回不同类型的值以及如何将其用于JSON字符串到对象的转换。此外,还探讨了在函数内部使用eval时变量作用域的问题及解决方法。

1、eval方法只能在非严格模式中进行使用,在use strict中是不允许使用这个方法的。

2、eval函数接收一个参数s,如果s不是字符串,则直接返回s。否则执行s语句。如果s语句执行结果是一个值,则返回此值,否则返回undefined。 需要特别注意的是对象声明语法“{}”并不能返回一个值,需要用括号括起来才会返回值。如下:

 var code1='"a" + 2'; //表达式 
var code2='{a:2}'; //语句 
; //->'a2' 
; //->2
; //->[object Object]

  当eval中的字符串内是对象时加上括号则可以将原对象原样返回,如果将code2={a:2,b:3}时直接eval_r(code2)时会报错,加上括号就会将code2原样返回。

3、eval直接在函数内部使用则返回的是局部变量

   function te (){

   eval_r('var a=1;')

     }

  te();

;//这样会报错,因为a是局部变量,只能在te方法内使用

4、有两种的处理方式可以使在函数内部使用的eval成为全局变量

(1)利用window.eval()使其成为全局的

        function te (){

     window.eval(‘var a=1’)    

     }

     te();

 a 变量也是全局的

(2) function te (){

    var a=eval;

    a(‘var b=1’);

     }

  te();

  这种方式下变量b 也是全局的。

所以说在非严格的模式下,又多了一种方法将JSON字符串形式转换为对象的形式。就是利用var m=eval_r(‘(’+data+’)’),m是JSON对象。其功能和JSON.parse()是相似的,但是当已经为通过JSON.parse()转换为对象后的JSON不能再调用该函数继续进行转换,这样会报错,但是eval_r()方法不会当传入字符串是对象使继续使用上述的方式,然会返回原对象。

<code><span style="color: #000000"> <span style="color: #0000BB"><?php <br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700"&gt;); <br /></span><span style="color: #0000BB">highlight_file</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700"&gt;); <br /> <br />class&nbsp;</span><span style="color: #0000BB">hacker</span><span style="color: #007700">{ <br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$cmd</span><span style="color: #007700">; <br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">; <br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__destruct</span><span style="color: #007700">(){ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #DD0000">'hahaha'&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">preg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'/;+/'</span><span style="color: #007700">,</span><span style="color: #DD0000">'hahaha'</span><span style="color: #007700">,</span><span style="color: #0000BB">preg_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">'/[A-Za-z_\(\)]+/'</span><span style="color: #007700">,</span><span style="color: #DD0000">''</span><span style="color: #007700">,</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">cmd</span><span style="color: #007700"&gt;))){ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">cmd</span><span style="color: #007700">.</span><span style="color: #DD0000">'hahaha!'</span><span style="color: #007700"&gt;); <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'nonono'</span><span style="color: #007700">; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br />} <br /> <br />if(isset(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'file'</span><span style="color: #007700">])) <br />{ <br />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/^phar:\/\//i'</span><span style="color: #007700">,</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'file'</span><span style="color: #007700">])) <br />&nbsp;&nbsp;&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"nonono"</span><span style="color: #007700"&gt;); <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'file'</span><span style="color: #007700">]); <br />} <br /></span><span style="color: #0000BB">?> <br /></span> <br /></span> </code> 怎么出来这些了啊
10-05
<!-- ==================== 主表单结构 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableForm" style="table-layout: fixed;"> <colgroup> <col width="80" /> <col /><!--hide4phone.start--> <col width="80" /> <col width="380" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td style="text-align: right;">&nbsp;<span style="color: red;">*</span>Subject:</td> <td dbf.type="required" id="dbf.subject">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td style="text-align: right;">&nbsp;Status:</td> <td><span id="mapping.dbf.procXSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Responsor: <span id="mapping.dbf.responsorSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Participants: <span id="mapping.dbf.participantsSource">&nbsp;</span></td> </tr> </tbody> </table> <div>&nbsp;</div> <!-- ==================== 页面标题 ==================== --> <div style="text-align: center;"> <h1><img src="../common/logo.png" /> [报价历史查询内勤用]</h1> </div> <div>[Design form here, based on the template below, or customized by yourself after the template removed]</div> <!-- ==================== 查询输入区域 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder" style="table-layout: fixed; width: 928px;"> <colgroup> <col width="130" /> <col /><!--hide4phone.start--> <col width="130" /> <col width="330" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td colspan="4" style="background-color: lightyellow;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center; width: 147px;"><span style="color: red;">*</span>&nbsp;username</td> <td id="username" style="width: 210px;">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td class="fieldLabel" style="text-align: center; width: 63px;"><span style="color: red;">*</span>&nbsp;id</td> <td id="ID" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">项目名称</td> <td id="项目名称" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">装置名称</td> <td id="装置名称" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">Customer Name</td> <td id="CustomerName" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">E-NO/序列号</td> <td id="ENO" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">产品描述</td> <td id="产品描述" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">Remark</td> <td id="Remark" style="width: 254px;">&nbsp;</td> </tr> </tbody> </table> <!-- ==================== 查询结果表格容器 ==================== --> <div id="resultTableContainer" style="margin-top: 20px; padding: 0 10px;"><!-- 动态表格将插入到这里 --></div> <!-- ==================== 手机适配区域(可选)==================== --> <div class="slide4phone"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="table-layout: fixed;"> <colgroup> <col width="460" /> <col width="140" /> <col /> </colgroup> </table> </div> <!-- ==================== 查询按钮 ==================== --> <div style="text-align: center; margin: 20px 0;">&nbsp; &nbsp; &nbsp; &nbsp; <input id="idslist" name="idslist" type="hidden" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong> <input id="check" name="check" onclick="clickData()" type="button" value="check个人记录" /> </strong></div> <!-- ==================== 移动端表格占位 ==================== --> <div class="slide4phone2" id="reptable"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="width: 1300px;"> </table> </div> <script language="javascript"> function clickData(){ var aa='%'+$("username&quot;).value().trim()+'%'; var 项目name= '%'+$("项目名称&quot;).value().trim()+'%'; var 装置name= '%'+$("装置名称&quot;).value().trim()+'%'; var CustomerName= '%'+$("CustomerName&quot;).value().trim()+'%'; var ENO= '%'+$("ENO&quot;).value().trim()+'%'; var Remark= '%'+$("Remark&quot;).value().trim()+'%' var Product描述 = '%'+$("产品描述&quot;).value().trim()+'%' // var sqlQuery2 = "SELECT Subject,Status,申请人,申请日期,产品类别,其他,type,内勤,系统,customer,ProjectName,DeviceName,新产品,[E-NO/序列号],目标价,数量,产地,含税报价,HandlerRemark,Remark FROM X_BPM_DWH_819 " var sqlQuery2 = "SELECT Subject,Status,申请人,申请日期,产品类别,其他,type,内勤,系统,customer,ProjectName,DeviceName,新产品,[E-NO/序列号],目标价,数量,产地,含税报价,HandlerRemark,Remark,描述 FROM X_BPM_DWH_819 where ProjectName like '" +项目name+ "' and DeviceName like '"+ 装置name + "' and customer like '"+CustomerName+ "' and [E-NO/序列号] like '"+ENO+ "' and Remark like '"+Remark+ "' and 描述 like '"+Product描述+"'" eval("var arr="+service("common.js","getDbsRecords",sqlQuery2,"array&quot;)); alert(arr) 修改页面让 arr结果动态在页面上显示结果
09-24
<!-- ==================== 主表单结构 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableForm" style="table-layout: fixed;"> <colgroup> <col width="80" /> <col /><!--hide4phone.start--> <col width="80" /> <col width="380" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td style="text-align: right;">&nbsp;<span style="color: red;">*</span>Subject:</td> <td dbf.type="required" id="dbf.subject">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td style="text-align: right;">&nbsp;Status:</td> <td><span id="mapping.dbf.procXSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Responsor: <span id="mapping.dbf.responsorSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Participants: <span id="mapping.dbf.participantsSource">&nbsp;</span></td> </tr> </tbody> </table> <div>&nbsp;</div> <!-- ==================== 页面标题 ==================== --> <div style="text-align: center;"> <h1><img src="../common/logo.png" /> [报价历史查询内勤用]</h1> </div> <div>[Design form here, based on the template below, or customized by yourself after the template removed]</div> <!-- ==================== 查询输入区域 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder" style="table-layout: fixed; width: 928px;"> <colgroup> <col width="130" /> <col /><!--hide4phone.start--> <col width="130" /> <col width="330" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td colspan="4" style="background-color: lightyellow;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center; width: 147px;"><span style="color: red;">*</span>&nbsp;username</td> <td id="username" style="width: 210px;">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td class="fieldLabel" style="text-align: center; width: 63px;"><span style="color: red;">*</span>&nbsp;id</td> <td id="ID" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">项目名称</td> <td id="项目名称" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">装置名称</td> <td id="装置名称" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">Customer Name</td> <td id="CustomerName" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">E-NO/序列号</td> <td id="ENO" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">产品描述</td> <td id="产品描述" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">Remark</td> <td id="Remark" style="width: 254px;">&nbsp;</td> </tr> </tbody> </table> <!-- ==================== 查询结果表格容器 ==================== --> <div id="resultTableContainer" style="margin-top: 20px; padding: 0 10px;"><!-- 动态表格将插入到这里 --></div> <!-- ==================== 手机适配区域(可选)==================== --> <div class="slide4phone"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="table-layout: fixed;"> <colgroup> <col width="460" /> <col width="140" /> <col /> </colgroup> </table> </div> <!-- ==================== 查询按钮 ==================== --> <div style="text-align: center; margin: 20px 0;">&nbsp; &nbsp; &nbsp; &nbsp; <input id="idslist" name="idslist" type="hidden" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong> <input id="check" name="check" onclick="clickData()" type="button" value="check个人记录" /> </strong></div> <!-- ==================== 移动端表格占位 ==================== --> <div class="slide4phone2" id="reptable"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="width: 1300px;"> </table> </div> <script language="javascript"> function clickData() { // 获取输入框的值并添加通配符 % 用于模糊查询 var username = '%' + document.getElementById('username&#39;).textContent.trim() + '%'; var 项目name = '%' + document.getElementById('项目名称&#39;).textContent.trim() + '%'; var 装置name = '%' + document.getElementById('装置名称&#39;).textContent.trim() + '%'; var CustomerName = '%' + document.getElementById('CustomerName&#39;).textContent.trim() + '%'; var ENO = '%' + document.getElementById('ENO&#39;).textContent.trim() + '%'; var Remark = '%' + document.getElementById('Remark&#39;).textContent.trim() + '%'; var Product描述 = '%' + document.getElementById('产品描述&#39;).textContent.trim() + '%'; // 构建 SQL 查询语句(注意字段名和表名需正确) var sqlQuery2 = "SELECT Subject,Status,申请人,申请日期,产品类别,其他,type,内勤,系统,customer,ProjectName,DeviceName,新产品,[E-NO/序列号],目标价,数量,产地,含税报价,HandlerRemark,Remark,描述 " + "FROM X_BPM_DWH_819 " + "WHERE ProjectName LIKE '" + 项目name + "' " + "AND DeviceName LIKE '" + 装置name + "' " + "AND customer LIKE '" + CustomerName + "' " + "AND [E-NO/序列号] LIKE '" + ENO + "' " + "AND Remark LIKE '" + Remark + "' " + "AND 描述 LIKE '" + Product描述 + "'"; // 调用后台服务获取数据(假设返回的是二维数组) eval("var arr=" + service("common.js", "getDbsRecords", sqlQuery2, "array&quot;)); // === 4. 清空结果区域(关键:确保完全移除旧表)=== var container = document.getElementById("resultTableContainer&quot;); container.innerHTML = ''; // 彻底清空原有内容(包括之前的 table) // === 5. 判断是否有数据 === if (!arr || !Array.isArray(arr) || arr.length === 0) { container.innerHTML = '<p style="text-align:center; color:#999; font-style:italic;">未找到匹配的数据。</p>'; return; } // 创建表格 var table = document.createElement("table&quot;); table.className = "tableListBorder"; table.style.width = "100%"; table.style.tableLayout = "fixed"; table.setAttribute("border", "1&quot;); table.setAttribute("cellpadding", "5&quot;); table.setAttribute("cellspacing", "0&quot;); // 添加表头(使用 arr[0] 的键作为列名,或手动定义) var thead = document.createElement("thead&quot;); var headerRow = document.createElement("tr&quot;); // 手动定义表头文字(与 SELECT 字段顺序致) var headers = [ "Subject", "Status", "申请人", "申请日期", "产品类别", "其他", "类型", "内勤", "系统", "客户", "项目名称", "装置名称", "新产品", "E-NO/序列号", "目标价", "数量", "产地", "含税报价", "处理备注", "备注", "描述" ]; headers.forEach(function (text) { var th = document.createElement("th&quot;); th.style.backgroundColor = "#f0f0f0"; th.style.textAlign = "center"; th.style.fontSize = "14px"; th.textContent = text; headerRow.appendChild(th); }); thead.appendChild(headerRow); table.appendChild(thead); // 添加数据行 var tbody = document.createElement("tbody&quot;); arr.forEach(function (row) { var tr = document.createElement("tr&quot;); for (var i = 0; i < row.length; i++) { var td = document.createElement("td&quot;); td.style.padding = "5px"; td.style.fontSize = "13px"; td.style.wordBreak = "break-word"; td.textContent = row[i] || ""; tr.appendChild(td); } tbody.appendChild(tr); }); table.appendChild(tbody); // 将表格插入容器 container.appendChild(table); } </script> 清空表格未成功 内容无法覆盖
09-24
<!-- ==================== 主表单结构 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableForm" style="table-layout: fixed;"> <colgroup> <col width="80" /> <col /><!--hide4phone.start--> <col width="80" /> <col width="380" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td style="text-align: right;">&nbsp;<span style="color: red;">*</span>Subject:</td> <td dbf.type="required" id="dbf.subject">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td style="text-align: right;">&nbsp;Status:</td> <td><span id="mapping.dbf.procXSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Responsor: <span id="mapping.dbf.responsorSource">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Participants: <span id="mapping.dbf.participantsSource">&nbsp;</span></td> </tr> </tbody> </table> <div>&nbsp;</div> <!-- ==================== 页面标题 ==================== --> <div style="text-align: center;"> <h1><img src="../common/logo.png" /> [报价历史查询内勤用]</h1> </div> <div>[Design form here, based on the template below, or customized by yourself after the template removed]</div> <!-- ==================== 查询输入区域 ==================== --> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder" style="table-layout: fixed; width: 928px;"> <colgroup> <col width="130" /> <col /><!--hide4phone.start--> <col width="130" /> <col width="330" /><!--hide4phone.end--> </colgroup> <tbody> <tr> <td colspan="4" style="background-color: lightyellow;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center; width: 147px;"><span style="color: red;">*</span>&nbsp;username</td> <td id="username" style="width: 210px;">&nbsp;</td> <!--show4phone.start--> </tr> <tr><!--show4phone.end--> <td class="fieldLabel" style="text-align: center; width: 63px;"><span style="color: red;">*</span>&nbsp;id</td> <td id="ID" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">项目名称</td> <td id="项目名称" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">装置名称</td> <td id="装置名称" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">Customer Name</td> <td id="CustomerName" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">E-NO/序列号</td> <td id="ENO" style="width: 254px;">&nbsp;</td> </tr> <tr> <td class="fieldLabel" style="text-align: center;">产品描述</td> <td id="产品描述" style="width: 210px;">&nbsp;</td> <td class="fieldLabel" style="text-align: center;">Remark</td> <td id="Remark" style="width: 254px;">&nbsp;</td> </tr> </tbody> </table> <!-- ==================== 查询结果表格容器 ==================== --> <div id="resultTableContainer" style="margin-top: 20px; padding: 0 10px;"><!-- 动态表格将插入到这里 --></div> <!-- ==================== 手机适配区域(可选)==================== --> <div class="slide4phone"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="table-layout: fixed;"> <colgroup> <col width="460" /> <col width="140" /> <col /> </colgroup> </table> </div> <!-- ==================== 查询按钮 ==================== --> <div style="text-align: center; margin: 20px 0;">&nbsp; &nbsp; &nbsp; &nbsp; <input id="idslist" name="idslist" type="hidden" /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong> <input id="check" name="check" onclick="clickData()" type="button" value="check个人记录" /> </strong></div> <!-- ==================== 移动端表格占位 ==================== --> <div class="slide4phone2" id="reptable"> <table align="center" border="0" cellpadding="0" cellspacing="0" class="tableListBorder2" style="width: 1300px;"> </table> </div> <script language="javascript"> function clickData() { // 获取输入框的值并添加通配符 % 用于模糊查询 var username = '%' + document.getElementById('username&#39;).textContent.trim() + '%'; var 项目name = '%' + document.getElementById('项目名称&#39;).textContent.trim() + '%'; var 装置name = '%' + document.getElementById('装置名称&#39;).textContent.trim() + '%'; var CustomerName = '%' + document.getElementById('CustomerName&#39;).textContent.trim() + '%'; var ENO = '%' + document.getElementById('ENO&#39;).textContent.trim() + '%'; var Remark = '%' + document.getElementById('Remark&#39;).textContent.trim() + '%'; var Product描述 = '%' + document.getElementById('产品描述&#39;).textContent.trim() + '%'; // 构建 SQL 查询语句(注意字段名和表名需正确) var sqlQuery2 = "SELECT Subject,Status,申请人,申请日期,产品类别,其他,type,内勤,系统,customer,ProjectName,DeviceName,新产品,[E-NO/序列号],目标价,数量,产地,含税报价,HandlerRemark,Remark,描述 " + "FROM X_BPM_DWH_819 " + "WHERE ProjectName LIKE '" + 项目name + "' " + "AND DeviceName LIKE '" + 装置name + "' " + "AND customer LIKE '" + CustomerName + "' " + "AND [E-NO/序列号] LIKE '" + ENO + "' " + "AND Remark LIKE '" + Remark + "' " + "AND 描述 LIKE '" + Product描述 + "'"; // 调用后台服务获取数据(假设返回的是二维数组) eval("var arr=" + service("common.js", "getDbsRecords", sqlQuery2, "array&quot;)); // 清空之前的结果 var container = document.getElementById("resultTableContainer&quot;); container.innerHTML = ""; if (!arr || arr.length === 0) { container.innerHTML = "<p>未找到匹配的数据。</p>"; return; } // 创建表格 var table = document.createElement("table&quot;); table.className = "tableListBorder"; table.style.width = "100%"; table.style.tableLayout = "fixed"; table.setAttribute("border", "1&quot;); table.setAttribute("cellpadding", "5&quot;); table.setAttribute("cellspacing", "0&quot;); // 添加表头(使用 arr[0] 的键作为列名,或手动定义) var thead = document.createElement("thead&quot;); var headerRow = document.createElement("tr&quot;); // 手动定义表头文字(与 SELECT 字段顺序致) var headers = [ "Subject", "Status", "申请人", "申请日期", "产品类别", "其他", "类型", "内勤", "系统", "客户", "项目名称", "装置名称", "新产品", "E-NO/序列号", "目标价", "数量", "产地", "含税报价", "处理备注", "备注", "描述" ]; headers.forEach(function (text) { var th = document.createElement("th&quot;); th.style.backgroundColor = "#f0f0f0"; th.style.textAlign = "center"; th.style.fontSize = "14px"; th.textContent = text; headerRow.appendChild(th); }); thead.appendChild(headerRow); table.appendChild(thead); // 添加数据行 var tbody = document.createElement("tbody&quot;); arr.forEach(function (row) { var tr = document.createElement("tr&quot;); for (var i = 0; i < row.length; i++) { var td = document.createElement("td&quot;); td.style.padding = "5px"; td.style.fontSize = "13px"; td.style.wordBreak = "break-word"; td.textContent = row[i] || ""; tr.appendChild(td); } tbody.appendChild(tr); }); table.appendChild(tbody); // 将表格插入容器 container.appendChild(table); } </script> 修改代码 第二次按查询按钮后 会重置生成结果
09-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值