jquery easyui grid 表格特殊字元處理

來源:互聯網
上載者:User

標籤:執行   address   測試   轉化   輸入   文本   返回   function   enter   

grid 擷取的資料中,如果資料存在測試的字元,或者js語句,會導致頁面配置錯亂,如下方法,讓擷取到的資料全部當成文本進行顯示

此操作主要防止以下亮點  

1. 由於業務需要,查詢的資料中存在特殊字元或者js語句,如:資料庫欄位中有一個欄位的內容是:  "alert(‘商品編碼‘);",那麼這個欄位查出來到表格中之後,頁面會彈出提示框

2.grid查詢的欄位來自於使用者手動輸入的文本,如果有惡意攻擊,直接輸入js語句,會執行相關語句。

 

表格欄位formatter 的時候調用 HTMLEncode

{field:‘dlAddress‘,title:‘使用地點‘,width: 200,align:‘center‘,

      formatter : function(value, row, index) {
          return HTMLEncode(value);
      }
}

 

/*-----------------------------------------------------------------------------------------*\
* 函數: 把特殊字元進行轉換
* 參數: value -- 需要轉化的字串
* 傳回值:
* 描述:
\*-----------------------------------------------------------------------------------------*/
function HTMLEncode(value) {
var returnValue;
if(value==null){
return null;
}
returnValue = value.replace(/&/g, ‘&‘);
returnValue = returnValue.replace(/</g, ‘&lt;‘);
returnValue = returnValue.replace(/>/g, ‘&gt;‘);

returnValue = returnValue.replace(/\n\n/g, ‘<br/>‘);
returnValue = returnValue.replace(/\r\r/g, ‘<br/>‘);
returnValue = returnValue.replace(/\n/g, ‘<br/>‘);
returnValue = returnValue.replace(/\r/g, ‘<br/>‘);
returnValue = returnValue.replace(/\t/g, ‘&nbsp;‘);
return returnValue;
}

jquery easyui grid 表格特殊字元處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.