检查这个例子。
它将更改标签的属性“名称”及其文本内容(读取其新的名称属性值)以查看更改的效果。
<script type="text/javascript">
function changeValue(select) {
var value = select.options[select.selectedIndex].value;
var label = document.getElementById('mylabel');
label.setAttribute('name', value);
label.innerHTML = label.getAttribute('name');
}
</script>
<select name="selectcity" onchange="changeValue(this);" >
<option>Mumbai</option>
<option>Delhi</option>
</select>
You selected: <label id="mylabel" name="citylabel"></label>
Run Code Online (Sandbox Code Playgroud)