简体   繁体   中英

jQuery changing select text

I try write function for cyrillization selected 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;    
}

What am I doing wrong here?

Try this:

var ddl=document.getElementById('idofselect');
    ddl.options[ddl.selectedIndex].text = text;
    ddl.options[ddl.selectedIndex].value = "";

See below a working solution which also receives as parameter the selector.

<!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>

You are not working the right way with selectors: First I have never seen that syntax of retrieving the selected options:

var text =(select option:selected).text;

Second you are trying to assign to a select the same select:

document.querySelector('#id5')=select;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM