2013-05-29 46 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'); 
}