Home >Web Front-end >JS Tutorial >JS method to change the option attribute in select_javascript skills
The example in this article describes the method of changing the option attribute in select using JS. Share it with everyone for your reference. The details are as follows:
Help a friend solve a small problem. The requirement is to change the text value displayed in the selected tab, and the new value is stored in a certain text box
Initial window:
<html> <head> <title>原窗口</title> <script> var parentValue=""; //全局变量,用于保存点击详情时select中指定opeion的值 function detail() { var select=document.getElementById('SHGX'); //获得select对象 parentValue=select.options[select.selectedIndex].text; window.open('详情窗口.html') } function updateSelect(childValue) { var select=document.getElementById('SHGX'); for(var i=0;i<select.length;i++) { if(select.options[i].text==parentValue) select.options[i].text=childValue; } } </script> </head> <body> <select id='SHGX'> <option value='111' title='夫'>夫</option> <option value='112' title='妻'>妻</option> <option value='120' title='子'>子</option> <option value='121' title='独生子'>独生子</option> <option value='122' title='继子'>继子</option> <option value='128' title='女婿'>女婿</option> </select> <button onclick="detail(); ">详情</button> </body> </html>
Details window:
<html> <head> <title>详情窗口</title> <script> function modify() { var childValue=document.getElementById('text_01').value; opener.updateSelect(childValue); //调用父窗口的函数 } </script> </head> <body> <input id="text_01" type="text" value=""/> <button onclick="modify();">修改</button> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming.