我在使用表单时遇到问题,并在该表单中使用 onchange 进行选择。当我在 2 选择之前启动表单时,它工作得很好,但是当我将表单移到表格之外(包括所有内容)时,onchange 将不起作用。关于为什么的任何想法?
这是我的表格:
<table border="0" class="signUp">
<tr><td align="center" class= "signUpfont"> Sign up for FREE </td></tr>
<tr><td><input type="text" text="First Name" class='signUpinput' id="fname" name="fname" placeholder="First Name" /></td></tr>
<tr><td><input type="text" class='signUpinput' id="lname" name="lname" placeholder="Last Name"/></td></tr>
<tr><td><input type="text" class='signUpinput' id="email" name="email" placeholder="Email"/></td></tr>
<tr><td><input type="text" class='signUpinput' id="email2" name="email2" placeholder="Re-enter Email"/></td></tr>
<tr><td><input type="password" class='signUpinput' id="pass" name="pass" placeholder="Password"/></td></tr>
<tr><td><form name="form1" action="submit.php" method='POST'>
<select name="country" onchange="window.getSchools()" style="color: white; background-color: #2B4478;">
<option value="0">Select State</option>
<option value="louisiana">Louisiana</option>
<option value="texas">Texas</option>
<option value="alabama">Alabama</option>
<option value="mississippi">Mississippi</option>
</select>
<br>
<select name="school" style="color: white;background-color: #2B4478;">
<option value="">Select School</option>
</select></td></tr>
<tr><td><input type="submit" name="submit" value="Sign Up" style="color:white; background-color:#2B4478; width: 100px;margin-left: 52px;"></td></tr>
</form>
</table>
这是我的 onchange 函数:
<script type="text/javascript">
function getSchools()
{
var xmlhttp;
try{
xmlhttp = new XMLHttpRequest;
}catch(e)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlhttp)
{
var form = document['form1'];
var country = form['country'].value;
xmlhttp.open("GET", "getSchools.php?country="+country, true);
xmlhttp.onreadystatechange = function ()
{
if(this.readyState == 4)
{
var s = document.createElement('select');
s.name = "school";
s.style.color = "white";
s.style.background = "#2b4478";
s.innerHTML = this.responseText;
if(form['school'])
{
form.replaceChild(s, form['school']);
}else
form.insertBefore(s, form['submit']);
}
}
xmlhttp.send(null)
}
}