日期:2014-05-17  浏览次数:20912 次

关于input文本框value带有引号显示错误问题???
我遇到的问题:我需要把一个变量值输入到一个text文本框里面,比如:
<input type="text" name="txtName" id="txtName" value="${entry.content}"/>
其中${entry.content}的值是从数据库循环出来的,里面的值可能带有引号,如果带有双引号和单引号的话,就会显示错误
我该怎么做才能让这个值显示正确呢?

如果这个变量显示需要用在js中的话,比如我点击某一文字链接,传递参数给js然后再在文本框中显示:
<a onclick="getRMouse('${entry }')">回复</a>

function getRMouse(param){
  alert(param);
  var content=document.getElementById("txt1").value=param; } 

如果只是传递文字和数字的话,是很正常的,如果所传递的值带有引号的话,就会显示错误,我该怎么解决呢?

------解决方案--------------------
可以考虑替换成HTML转义字符:&quot;
------解决方案--------------------
查询的时候,对你查询的字段这样处理replace(查询的字段,'"','&quot;')
------解决方案--------------------
Java code

    public static String replaceString(String strData, String regex, String replacement)
    {
        if (strData == null)
        {
            return null;
        }
        int index;
        index = strData.indexOf(regex);
        String strNew = "";
        if (index >= 0)
        {
            while (index >= 0)
            {
                strNew += strData.substring(0, index) + replacement;
                strData = strData.substring(index + regex.length());
                index = strData.indexOf(regex);
            }
            strNew += strData;
            return strNew;
        }
        return strData;
    }