2013-05-29 47 views
1

我有一个选择框是有特殊字符不工作

<select> 
<option value="">--Select--</option> 
<option id="402883273d3fe2bd013d49db55ca0007" value="~`[email protected]#$%^&*()_+-={}|[]\:" title="~`[email protected]#$%^&*()_+-={}|[]\:">~`[email protected]#$%^&amp;*()_+-={}|[]\:</option> 
<option id="402883273b660e71013b6ff04187000d" value="asdasd" title="asdasd">asdasd</option> 
<option id="402883273952e67f01395332fd5f0006" value="CC-1" title="CC-1">CC-1</option> 
<option id="402883273bdb9824013c14d3c2c30007" value="rto" title="rto">rto</option> 
<option id="402883273bdb9824013c14d5decf0008" value="xc" title="xc">xc</option> 
</select> 

,并选择现在当我加载的页面,并选择与价值选择=“〜`!@#$%^ & *( )_ + - = {} | [] \:“ 未被选中。对于选择

代码:

var selectedCategory = "<%=StringEscapeUtils.escapeJavaScript(toEditClause.getClauseGroup().getClauseCategory().getName())%>"; 
     console.log("before : "+selectedCategory); 
     selectedCategory = selectedCategory.replace(/'/g,"\\'"); 
     console.log("After : "+selectedCategory); 
     jQuery("#clauseCategory_ option[value='"+selectedCategory+"']").attr("selected", "selected"); 

我认为问题是由于特殊字符,否则如果让我选择一些其他的价值,如:“CC-1” ..其工作的罚款

+0

是否有其他方式可以选择该选项?通过一个类也许,或索引?你也可以尝试通过'val()'设置值:'jQuery(“#clauseCategory _”)。val(selectedCategory;' –

+0

尝试使用urlencoding的值。 – snitch182

+0

使用'\\\'转义特殊字符 –

回答

1

相反的价值去为ID ..即在ID的基础选择..所以用这个jQuery("#clauseCategory_ option[id='"+selectedCategory+"']").attr("selected", "selected");

1

你可以使用此solution

// Escapes special characters and returns a valid jQuery selector 
function jqSelector(str) 
{ 
    return str.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1'); 
}