我在这里问一个关于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="">
`
答案 0 :(得分:1)
这样做:
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="">
答案 1 :(得分:1)
尝试一个简单的代码希望帮助
<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="">
答案 2 :(得分:1)
在click =&#34;&#34;
中添加以下代码var e = document.getElementById(&#39; district&#39;); document.getElementById(&#39; txtresult&#39;)。value = e.options [e.selectedIndex] .value;
底部文本框中有两个Id属性,请删除id =&#34;添加&#34;。
希望这有帮助。
答案 3 :(得分:1)
试试这个: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;
}