这是使用select的工作形式,但是如何使它在形式上工作相同?
<select id="destination" name="destination" onchange="navigate(this.options[this.selectedIndex].value)">
<option value="">Select country</option>
<option value="afghanistan">Afghanistan</option>
<option value="albania">Albania</option>
<option value="algeria">Algeria</option>
<option value="andorra">Andorra</option>
<option value="angola">Angola</option>
的script.js
function navigate(destination)
{
if (destination != "")
location.href = destination + ".php";
}
无论如何,它仍然适用于&#34; datalist&#34;?
<datalist id="countries" >
<option value="Afghanistan">
<option value="Albania">
<option value="United Kingdom">
<option value="United States">
<option value="Vanuatu">
<option value="Vatican City">
<option value="Yemen">
<option value="Zambia">
<option value="Zimbabwe">
</datalist>
答案 0 :(得分:1)
要通过JavaScript在onsubmit
处理程序中获取表单值,您可以将id
分配给input
元素,然后以编程方式读取其value
。
function myFunction() {
// 1. use the id 'txtCountry' to get reference to the INPUT element
// Note: you need to edit your HTML to assign the id 'txtCountry' (see sample HTML below)
var txtCountry = document.getElementById('txtCountry');
// 2. assign the value (text) in the INPUT element to a variable called 'destination'
var destination = txtCountry.value;
// 3. redirect to an URL based on the value from step 2
var url = destination + ".php";
alert("You are about to redirect to\n\n" + url);
location.href = url;
}
&#13;
<form onsubmit="myFunction()">
Enter country:
<input type="text" id="txtCountry" name="txtCountry" list="countries">
<input type="submit" value="Submit">
</form>
<datalist id="countries">
<option value="Afghanistan">
<option value="Albania">
<option value="United Kingdom">
<option value="United States">
<option value="Vanuatu">
<option value="Vatican City">
<option value="Yemen">
<option value="Zambia">
<option value="Zimbabwe">
</datalist>
&#13;