我尝试编写cyrillization选择select.option.text。
的函数function latin_to_cyrill(id5){
var select = document.querySelector('#id5');
var text =(select option:selected).text;
var arrru = new Array ('Я','я','Ю','ю','Ч','ч','Ш','ш','Щ','щ','Ж','ж','А','а','Б','б','В','в','Г','г','Д','д','Е','е','Ё','ё','З','з','И','и','Й','й','К','к','Л','л','М','м','Н','н', 'О','о','П','п','Р','р','С','с','Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ы','ы','Ь', 'ь','Э','э');
var arren = new Array ('Ya','ya','Yu','yu','Ch','ch','Sh','sh','Sh','sh','Zh','zh','A','a','B','b','V','v','G','g','D','d','E','e','E','e','Z','z','I','i','J','j','K','k','L','l','M','m','N','n', 'O','o','P','p','R','r','S','s','T','t','U','u','F','f','H','h','C','c','Y','y','\'','\'','E', 'e');
for(var i=0; i<arren.length; i++){
var reg = new RegExp(arren[i], "g");
text = text.replace(reg, arrru[i]);
(select option:selected).text=text;
}
document.querySelector('#id5')=select;
}
我在这里做错了什么?
答案 0 :(得分:0)
试试这个:
var ddl=document.getElementById('idofselect');
ddl.options[ddl.selectedIndex].text = text;
ddl.options[ddl.selectedIndex].value = "";
答案 1 :(得分:0)
请参阅下面的工作解决方案,该解决方案还接收选择器作为参数。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script>
function latin_to_cyrill(selector){
var select = document.querySelector(selector);
var selIndex = select.selectedIndex;
//Retrieve the text from the selected option
var text = select.options[selIndex].text;
var arrru = new Array ('Я','я','Ю','ю','Ч','ч','Ш','ш','Щ','щ','Ж','ж','А','а','Б','б','В','в','Г','г','Д','д','Е','е','Ё','ё','З','з','И','и','Й','й','К','к','Л','л','М','м','Н','н', 'О','о','П','п','Р','р','С','с','Т','т','У','у','Ф','ф','Х','х','Ц','ц','Ы','ы','Ь', 'ь','Э','э');
var arren = new Array ('Ya','ya','Yu','yu','Ch','ch','Sh','sh','Sh','sh','Zh','zh','A','a','B','b','V','v','G','g','D','d','E','e','E','e','Z','z','I','i','J','j','K','k','L','l','M','m','N','n', 'O','o','P','p','R','r','S','s','T','t','U','u','F','f','H','h','C','c','Y','y','\'','\'','E', 'e');
for(var i=0; i<arren.length; i++){
var reg = new RegExp(arren[i], "g");
text = text.replace(reg, arrru[i]);
}
//Put back the mofified text to the selected option
select.options[selIndex].text = text;
}
</script>
</head>
<body>
<select id="id5">
<option value="1">First Text</option>
<option value="2">Second Text</option>
</select>
<input type=button onclick="latin_to_cyrill('#id5')" value="Run">
</body>
</html>
您没有以正确的方式使用选择器: 首先,我从未见过检索所选选项的语法:
var text =(select option:selected).text;
其次,您尝试分配选择相同的选择:
document.querySelector( '#ID5')=选择;