我们正在开发一个使用 struts 2、JSP 的项目,并且为了进行验证,我们使用 JavaScript 来解决我的问题,在某些情况下,我们在 Load() 方法的页面上通过 JavaScript 设置文本框值。因为我们正在编码字符串以避免脚本错误,接下来我们将该值设置为文本框。但我想在将文本框值设置为文本框之前对其进行解码,然后在解码后我们需要在文本框中显示。提前致谢。
代码是:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dijit/themes/claro/claro.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/resources/Grid.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/resources/tundraGrid.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/enhanced/resources/claro/EnhancedGrid.css"/>
<link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css"/>
<link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/widget/ColorPicker/ColorPicker.css" />
<link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/form/resources/FileInput.css" />
<script type="text/javascript" djConfig="parseOnLoad: true, isDebug: false" src="/cxp/scripts/dojo 1.6/dojo/dojo.js"></script>
<script type='text/javascript' src='/cxp/scripts/jquery/libs/jquery-1.4.4.min.js'></script>
<script type='text/javascript' src='/cxp/scripts/jquery/libs/jquery-ui-1.8.11.custom.min.js'></script>
<script type="text/javascript" src="/cxp/scripts/jquery/libs/date.js"></script>
<script type='text/javascript' src='/cxp/scripts/jquery/jquery.weekcalendar.js'></script>
<script type='text/javascript' src='/cxp/scripts/jquery/jquery-dateformat.js'></script>
<link rel='stylesheet' type='text/css' href='/cxp/scripts/jquery/libs/css/smoothness/jquery-ui-1.8.11.custom.css' />
<link rel="stylesheet" type="text/css" href="/cxp/styles/styles.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/styles/wareHouseStyles.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/styles/maintenance.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/styles/dojostyles.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/styles/supervisor.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/styles/warehouse.css"></link>
<link rel="stylesheet" type="text/css" href="/cxp/styles/alignment.css"></link>
<script type="text/javascript" src="/cxp/scripts/js/popup.js"></script>
<script type="text/javascript" src="/cxp/scripts/js/date.js"></script>
<script type="text/javascript" src="/cxp/scripts/js/validations.js"></script>
<script type="text/javascript" src="/cxp/scripts/js/dojovalidations.js"></script>
<script type="text/javascript" src="/cxp/scripts/js/reports.js"></script>
<script type="text/javascript" src="/cxp/scripts/js/formateDate.js"></script>
<script type="text/javascript" src="/cxp/scripts/jspspellcheck/spellcheck-caller.js"></script>
</head>
<head>
<title>CaterXpert - Maintenance</title>
<script type="text/javascript">
dojo.require("dijit.form.Form");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.RadioButton");
dojo.require("dijit.form.Select");
dojo.require("dojo.parser");
var res;
dojo.addOnLoad (function() {
var id="54";
/* var id1="lookupBean [Type=""#***^&&&'''#$^%''&$#@"L:"?>aa, lookupId=54, Status=null, SortOrder=1, Active=1, Days=null, category=null, name=null, unitTypeId=null, invSupplierId=null, beverageCategoryID=null, cost=null, price=null, description=null, unitId=null, caterDocsId=null, sOrder=0, lookUpActive=null, taxType=null]"; */
if(id >0 && id !=""){
dojo.byId("Type1").value=decodeTags("""#***^&&&'''#$^%''&$#@"L:"?>aa");
dojo.byId("SortOrder2").value="1";
}
if(id > 0 && id !=""){
var active = '1';
if(active ==1){
dijit.byId("active").setValue(true);
}
else if(status ==0){
dijit.byId("inactive").setValue(true);
}
}
if (document.pub) {
document.pub();
}
});
function decodeTags(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
// add whatever you want to replace.
}
function submitForm(){
if(validateAll()){
var type = dojo.byId("type").value;
document.getElementById("lookupId").value=1;
document.forms[0].action = "saveSystemSettingLookup.action?type="+type;
document.forms[0].submit();
}
}
function validateAll(){
var temp=true;
if(dojo.byId("Type1").value==""){
alert("Empty Type");
temp=false;
}
if(res>0){
alert('DUPLICATE TYPE TRY AGAIN');
temp=false;
}
var sortOrder= dojo.byId("SortOrder2").value;
if(sortOrder !=""){
var onlyNums = /[^0-9]/g;
if (onlyNums.test(sortOrder)) {
alert(" Invalid Sort Order");
temp= false;
}
}
return temp;
}
function doClose()
{
var lookUpId = dojo.byId("id").value;
var type = dojo.byId("type").value;
if(lookUpId > 0){
parent.view.location.replace("/cxp/corpmaintenance/viewSystemSettingLookup.action?type="+type);
}
else{
window.parent.location="/cxp/corpmaintenance/viewCorpMaintenanceFrame.action";
}
}
function duplicateChecking()
{
res = 0;
var type = dojo.byId("type").value;
var id = document.getElementById("id").value;
var columnName=document.getElementById("columnName").value;
var idValue1=document.getElementById("idValue").value;
var name = document.getElementById(idValue1).value;
/* This code is for Ajax*/
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
res = eval( "("+ xmlhttp.responseText +")" );
submitForm();
}
}
xmlhttp.open("GET","/cxp/corpmaintenance/checkSystemSettingLookup.action?columnName="+columnName+"&name="+name+"&type="+type+"&id="+id+"&rand="+Math.random(),true);
xmlhttp.send(null);
}
</script>
</head>
<body class="claro">
<div>
<form name="bean" action="" id="bean">
<input name="id" id="id" type="hidden" value="54" />
<input name="type" id="type" type="hidden" value="DeliveryType" />
<input type="hidden" name="lookupId" id ="lookupId"/>
<input type="hidden" name="formName" id="formName" value=""></input>
<table class="formContentPane column-width35" align="center">
<tr><td> </td></tr>
<tr>
<td class="paddingleftfive" style="width: 5%" nowrap="nowrap"><label>Type</label></td>
<td class="mandatoryField" style="width: 3px">
</td>
<td >
<input dojoType="dijit.form.TextBox" id="Type1" name="bean.Type" size="0" maxlength="50"/>
<input type="hidden" id="columnName" name="columnName" value="Type"></input>
<input type="hidden" id="idValue" name="idValue" value="Type1"></input>
</td>
</tr>
<tr>
<td class="paddingleftfive" style="width: 5%" nowrap="nowrap"><label>SortOrder</label>
</td>
<td> </td>
<td >
<input dojoType="dijit.form.TextBox" id="SortOrder2"
name="bean.SortOrder" size="0" maxlength="5"/>
<input type="hidden" id="columnName" name="columnName" value="SortOrder"></input>
<input type="hidden" id="idValue" name="idValue" value="SortOrder2"></input>
</td>
</tr>
<tr>
<td style="width: 5%" nowrap="nowrap" class="paddingleftfive"><label>Active</label></td>
<td> </td>
<td>
<input dojoType="dijit.form.RadioButton" checked="checked" id="active" name="bean.active" type="radio" value="1"/>
<label>Yes</label>
<input dojoType="dijit.form.RadioButton" name="bean.active" id="inactive" type="radio" value="0"/>
<label>No</label>
</td>
</tr>
<tr></tr>
<tr><td> </td></tr>
</table>
<table class="saveclosebuttons" align="center" >
<tr>
<td align="center">
<button dojoType="dijit.form.Button" onclick="duplicateChecking()" class="mediumbutton" type="button">
Save</button>
<button dojoType="dijit.form.Button" onclick="doClose()" class="mediumbutton" type="button">
Close</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
最佳答案
不确定是否要将 < 转换为 <反之亦然,但您可以在 JavaScript 中编写一个函数来解码所有不需要的字符。
function decodeTags(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
// add whatever you want to replace.
}
编辑我原来的答案。感谢您提供所有详细信息和详细的 html 代码。看起来我们需要做与我们所做的相反的事情。
function decodeTags(str) {
return str
.replace(/"/g, "\"")
.replace(/&/g, "&")
.replace(/'/g, "'")
.replace(/>/g, ">")
.replace(/</g, "<");
// add whatever you want to replace.
}
关于javascript - 如果字符串来自带有编码的 javascript,我如何解码 html 文本框的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21900037/