我正在尝试验证(如果它们有价值)我的html的第一个两个下拉列表。在单击按钮时,getURL
我正在验证下拉列表中是否有任何选定的值,如果是,我想继续function getURL
。
但在验证之后不久,如果第二个保管箱中没有值,则控件不会停止。它继续进行并显示URL
。
我只想显示前URL
两个下拉列表中的值。但是现在有了下面的代码,我alert:url
很快就会得到alert:select product version
Javascript:
function validate() {
var pname = document.getElementById("selProductName");
if(pname.selectedIndex == 0) {
alert('select product name');
}
var pversion = document.getElementById("selProductVersion");
if(pversion.selectedIndex == 0) {
alert('select product version');
return false;
}
return true;
}
function getURL() {
validate();
// I have code here to create desired URL
alert("url:" + url);
}
HTML:
Product Name:
<select id="selProductName" name="selProductName" onchange="changeProductName(this.value);">
<option>--Choose Product Name--</option>
</select>
<br>Product Version:
<select id="selProductVersion" name="selProductVersion" onchange="changeProductVersion(this.value);">
</select>
<br>File Name:
<select id="selFileName" name="selFileName"></select>
<br><button onclick="getURL()">Get URL</button>
validate()
执行并返回false。好的。接着?然后,脚本继续运行并警告URL。
目前 :
function getURL() {
validate(); // Returns true or return false, no consequence.
alert("url:" + url); // Always happens.
}
您应该使用:
function getURL() {
if(!validate()) return; // Blocks execution if validation fails.
alert("url:" + url);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句