簡體   English   中英

檢查是否在不使用jquery的情況下未在選擇框中明確選擇任何選項?

[英]Check that no option was explicitly selected in a select box without using jquery?

//如何檢查選擇框中是否沒有明確選擇任何選項? 我使用以下代碼,它可以工作,但是有沒有更優雅的方法來檢查是否在不使用jquery的情況下在選擇框中沒有顯式選擇任何選項?

    // This function is called onclick or onchange a select in the submitting form
    function getplano(aaaooo){
     var esepen = document.getElementById('esEpendysi');
     var exepen=document.getElementById('exEpendysi');
    // (esEpendysi and  exEpendysi are two other selects in the submitting form)
     try{
     var esepval = esepen.options[esepen.selectedIndex].innerHTML;
    // or  ////~ var esepval = esepen.options[esepen.selectedIndex].text;   
    }
    catch(e){
    // alert(e.name + "\n" + e.message)
     //When there is not a selected value the error thrown is:
     // NS_ERROR_DOM_INDEX_SIZE_ERR
    //Index or size is negative or greater than the allowed amount
    }
    try{
      var exepval = exepen.options[exepen.selectedIndex].innerHTML; 
    }
    catch(e){
    // alert(e.name + "\n" + e.message)
    }
    if(esepval && exepval) {
    alert(aaaooo +'&esepen='+esepval+'&exepen='+exepval);
//example: handling.php?tzapan=panel&esepen=aluminium&exepen=wood
    }
     else
        alert('Please select Outside or/and Inside surface finish');
    }

html格式為:

    <BODY >
    <form name="" action="" method="post">
    <table >
    <tr><td align=left>Outside finish:<br><SELECT size='2'  name=exEpendysi  id='exEpendysi'  ><OPTION>aluminium</OPTION><OPTION>wood</OPTION></SELECT></td><td align=left>Interior finish:<br><SELECT size='2'  name=esEpendysi  id='esEpendysi' ><OPTION>aluminium</OPTION><OPTION>wood</OPTION></SELECT></td></tr>
    <tr><td style='font-size:16px;font-weight:bold;'>fixed side<br><select  size='2' id='selectplainou' name='selectplainou' onChange="getplano('handling.php?tzapan='+this.options[this.selectedIndex].innerHTML)" > <OPTION>panel</OPTION><OPTION>glass</OPTION> </select ></td>
    <td><input type="submit" value="Submit" /></td></tr>
    </table>
    </form>
    </BODY></HTML>  
function getplano( aaaooo ) {
    var esepen = document.getElementById( 'esEpendysi' );
    var exepen = document.getElementById( 'exEpendysi' );

    // These two will return true if it's the default value
    // Else, it returns false.
    var esepenSelected = esepen.options[ esepen.selectedIndex ].value === "default option";
    var exepenSelected = exepen.options[ exepen.selectedIndex ].value === "default option";

    // Since we have these two variables, we can elegantly use:
    if ( !esepenSelected && !exepenSelected ) {
        // do something
    }
}

夠優雅嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM