我在这里问一个关于JavaScript的问题。我是 JavaScript 的新手,所以如果我出错了,请原谅。有人可以帮助我获得结果的值吗?从下拉列表中选择任何状态后,"显示"按钮应为我提供状态的值作为结果。可能吗?
<h1>Please select a State</h1>
<select id = "district">
<option value = "Tiruvanandapuram">Kerala</option>
<option value = "Chennai">Tamil Nadu</option>
<option value = "New Delhi">Delhi</option>
</select>
<input type = "button" value = "Show" onclick = "" />
<INPUT type="text" ID="add" id="txtresult" NAME="result" VALUE="">
'
这将做到这一点:
var sel = document.getElementById("district");
function show(){
var txt = document.getElementById("add");
txt.value = sel.options[sel.selectedIndex].value;
}
<h1>Please select a State</h1>
<select id = "district">
<option value = "Kerala">Kerala</option>
<option value = "Chennai">Tamil Nadu</option>
<option value = "New Delhi">Delhi</option>
</select>
<input type = "button" value = "Show" onclick = "show()" />
<INPUT type="text" id="add" id="txtresult" NAME="result" VALUE="">
尝试一个简单的代码希望有帮助
<script type="text/javascript">
function run() {
document.getElementById("srt").value = document.getElementById("district").value;
}
</script>
<h1>Please select a State</h1>
<select id = "district">
<option value="Tiruvanandapuram">Kerala</option>
<option value="Chennai">Tamil Nadu</option>
<option value="Delhi">Delhi</option>
</select>
<input type = "button" value = "Show" onclick="run()"/>
<INPUT type="text" ID="srt" id="txtresult" NAME="result" VALUE="">
在 click=" 中添加以下代码段
var e = document.getElementById('district');d ocument.getElementById('txtresult').value= e.options[e.selectedIndex].value;
底部文本框中有两个Id属性,请删除id="添加"。
希望这有帮助。
试试这个:HTML ---
<h1>Please select a State</h1>
<select id="district">
<option value="Tiruvanandapuram">Kerala</option>
<option value="Chennai">Tamil Nadu</option>
<option value="New Delhi">Delhi</option>
</select>
<input type= "button" value= "show" id="btn" onclick= show_district() />
<input type="text" id = sss value = "" />
JavaScript :
function show_district() {
var selector = document.getElementById('district');
var value = selector[selector.selectedIndex].value;
document.getElementById('sss').value = value;
}