0

我正在通过 Struts HTML 标记为特定的 jsp 使用选择选项。选项的值为 Yes 和 No。这是代码。

  <select name="select" id='choice'>
     <option value="<%YES%>" selected><%="YES"%></option>
     <option value="<%=NO%>"></option>  

默认情况下,在选择选项列表中看到的值为 YES。我正在执行验证,如果没有选择任何值(如在“是”上给出的 Selected 属性)并且提交了表单,则应引发警报以选择新值。下面是我的代码。

 if( document.form.select.value == "YES" )
 {
   alert( "Please select other value" );
   return false;
 }
 if( document.form.select.value == "NO" )
 {
   alert( "Please select other value" );
   return false;
 }

上面的代码没有正确验证。谁能建议我一些改变。谢谢

4

1 回答 1

1

这是检查所选值是否为默认值的方法,即使我不太了解您的意图。

el = document.getElementById('choice');

if( el.options[el.selectedIndex].defaultSelected){
    alert("Please select other value");
    return false;
}

PS:您可能需要修复您的代码:

<select name="select" id='choice'>
     <option value="<%=YES%>" selected><%="YES"%></option>
     <option value="<%=NO%>"></option>
</select>
于 2013-03-07T21:45:03.340 回答