我正在尝试创建一个表单,如果传递了“0”的值,则会引发错误。我很难过,真的可以使用一些帮助。此脚本中有两个函数。第一个是工作,我可以根据选择更改 URL。我遇到问题的地方是我希望选择“--”和“选择状态”来触发第二个脚本,理论上该脚本会突出显示该字段并且不允许用户单击。这是名为“validateEmpty”的函数
我对另一种方法完全持开放态度,这是我发现的教程的破解版本,它传递了 value.length 为 0。
示例页面在这里: http ://www.brimbar.com/no_crawl/js-test-homepage.html
或者
代码如下:
<code>
<div id="quoteDropdowns">
<select id="stateType" name="stateType" onchange="dosomething()">
<option value="select" selected="selected">Choose State</option>
<option value="IL">Illinois</option>
<option value="MD">Maryland</option>
<option value="TX">Texas</option>
<option value="VA">Virginia</option>
<option value="--">--</option>
<option value="other">Other</option>
</select>
</div>
<div id="getAQuote"> <a id="mylink" class="myButton" href="#" onclick="validateEmpty">Get a Quote!</a> </div>
<script type="text/javascript">
function dosomething() {
var inputSelector = document.getElementById("stateType");
var link = document.getElementById("mylink");
if (inputSelector.value == "VA") {
link.href = "http://www.test1.com";
}
else if (inputSelector.value == "MD") {
link.href = "http://www.test2.com";
}
else if (inputSelector.value == "IL") {
link.href = "http://www.test3.com";
}
else if (inputSelector.value == "TX") {
link.href = "http://www.test4.com";
}
else if (inputSelector.value == "other") {
link.href = "http://www.test5.com";
}
else if (inputSelector.value == "--") {
stateType.value.length == 0;
link.href = "#";
}
}
console.log('First Function is Valid');
function validateEmpty(stateType) {
var error = value.length==0;
if (stateType.value.length == 0) {
stateType.style.border = 'red';
error = "The required field has not been filled in.\n"
} else {
stateType.style.background = 'White';
}
return error;
}
console.log('Second Function is Valid');
</script></code>