我現在正在用下面的問題解決問題,並且我已經沒有想法了。從來就一個選擇框與具有HTML字符在他們的價值領域不同選項:當值爲HTML時,通過它的值選擇一個元素字符
<select>
<option value="Zinksulfat" selected="selected">Zinksulfat</option>
<option value="Zypressenöl" selected="selected">Zypressenöl</option>
<option value="Wirkstoff"Mit'Anführungszeichen" selected="selected">Wirkstoff"Mit'Anführungszeichen</option>
</select>
爲了使用這個簡單的功能的jQuery I'm變量HTML編碼:
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace('ü', 'ü').replace('ö', 'ö');
}
現在我試圖通過它的值(「選擇」是選擇框)獲得選項對象:
// Set the element value
var elementValue = 'Wirkstoff"Mit'Anführungszeichen'; // Invalid I know - only for description purpose
elementValue = htmlEntities(elementValue);
return select.find('option[value="'+elementValue+'"]')[0];
因此,對於沒有HTML字符的元素,它就像一個魅力(例如。 「Zinksulfat」)。但對於具有HTML字符的元素,它不起作用。雖然「elementValue」中的值和值字段完全相同,但我收到「undefined」。
有沒有人知道要做什麼神奇的事情,以便我可以在選擇器中使用HTML字符?
由於提前, 邁克爾
只是逃避correctly.refer http://jsfiddle.net/769gp0nn/ –
我的回答是矯枉過正(刪除)字符串..只需將@VJ Sai建議的正確格式化爲字符串:) –
@VJ Sai:將您的評論發佈爲答案(在別人之前) –