0

0 から 23 までの値を選択する 2 つのドロップダウン リストがあります。ユーザーが最初のリストから値を選択すると、2 番目のリストでこの値より低い値を選択できないようにする必要があります。

ここでJavaScriptを介して検証を追加するにはどうすればよいですか? !!

<select name="hourfrom">
    <option value="0" @(Request["hourfrom"] == "0" ? " selected=\"selected\"" : "") >0</option>
    <option value="1" @(Request["hourfrom"] == "1" ? " selected=\"selected\"" : "") >1</option>
    <option value="2" @(Request["hourfrom"] == "2" ? " selected=\"selected\"" : "") >2</option>
    <option value="3" @(Request["hourfrom"] == "3" ? " selected=\"selected\"" : "") >3</option>
    <option value="4" @(Request["hourfrom"] == "4" ? " selected=\"selected\"" : "") >4</option>
    <option value="5" @(Request["hourfrom"] == "5" ? " selected=\"selected\"" : "") >5</option>
    <option value="6" @(Request["hourfrom"] == "6" ? " selected=\"selected\"" : "") >6</option>
    <option value="7" @(Request["hourfrom"] == "7" ? " selected=\"selected\"" : "") >7</option>
    <option value="8" @(Request["hourfrom"] == "8" ? " selected=\"selected\"" : "") >8</option>
    <option value="9" @(Request["hourfrom"] == "9" ? " selected=\"selected\"" : "") >9</option>
    <option value="10" @(Request["hourfrom"] == "10" ? " selected=\"selected\"" : "") >10</option>
    <option value="11" @(Request["hourfrom"] == "11" ? " selected=\"selected\"" : "") >11</option>
    <option value="12" @(Request["hourfrom"] == "12" ? " selected=\"selected\"" : "") >12</option>
    <option value="13" @(Request["hourfrom"] == "13" ? " selected=\"selected\"" : "") >13</option>
    <option value="14" @(Request["hourfrom"] == "14" ? " selected=\"selected\"" : "") >14</option>
    <option value="15" @(Request["hourfrom"] == "15" ? " selected=\"selected\"" : "") >15</option>
    <option value="16" @(Request["hourfrom"] == "16" ? " selected=\"selected\"" : "") >16</option>
    <option value="17" @(Request["hourfrom"] == "17" ? " selected=\"selected\"" : "") >17</option>
    <option value="18" @(Request["hourfrom"] == "18" ? " selected=\"selected\"" : "") >18</option>
    <option value="19" @(Request["hourfrom"] == "19" ? " selected=\"selected\"" : "") >19</option>
    <option value="20" @(Request["hourfrom"] == "20" ? " selected=\"selected\"" : "") >20</option>
    <option value="21" @(Request["hourfrom"] == "21" ? " selected=\"selected\"" : "") >21</option>
    <option value="22" @(Request["hourfrom"] == "22" ? " selected=\"selected\"" : "") >22</option>
    <option value="23" @(Request["hourfrom"] == "23" ? " selected=\"selected\"" : "") >23</option>

    </select>

に:

    <select name="hourto">
      <option value="0" @(Request["hourto"] == "0" ? " selected=\"selected\"" : "") >0</option>
    <option value="1" @(Request["hourto"] == "1" ? " selected=\"selected\"" : "") >1</option>
    <option value="2" @(Request["hourto"] == "2" ? " selected=\"selected\"" : "") >2</option>
    <option value="3" @(Request["hourto"] == "3" ? " selected=\"selected\"" : "") >3</option>
    <option value="4" @(Request["hourto"] == "4" ? " selected=\"selected\"" : "") >4</option>
    <option value="5" @(Request["hourto"] == "5" ? " selected=\"selected\"" : "") >5</option>
    <option value="6" @(Request["hourto"] == "6" ? " selected=\"selected\"" : "") >6</option>
    <option value="7" @(Request["hourto"] == "7" ? " selected=\"selected\"" : "") >7</option>
    <option value="8" @(Request["hourto"] == "8" ? " selected=\"selected\"" : "") >8</option>
    <option value="9" @(Request["hourto"] == "9" ? " selected=\"selected\"" : "") >9</option>
    <option value="10" @(Request["hourto"] == "10" ? " selected=\"selected\"" : "") >10</option>
    <option value="11" @(Request["hourto"] == "11" ? " selected=\"selected\"" : "") >11</option>
    <option value="12" @(Request["hourto"] == "12" ? " selected=\"selected\"" : "") >12</option>
    <option value="13" @(Request["hourto"] == "13" ? " selected=\"selected\"" : "") >13</option>
    <option value="14" @(Request["hourto"] == "14" ? " selected=\"selected\"" : "") >14</option>
    <option value="15" @(Request["hourto"] == "15" ? " selected=\"selected\"" : "") >15</option>
    <option value="16" @(Request["hourto"] == "16" ? " selected=\"selected\"" : "") >16</option>
    <option value="17" @(Request["hourto"] == "17" ? " selected=\"selected\"" : "") >17</option>
    <option value="18" @(Request["hourto"] == "18" ? " selected=\"selected\"" : "") >18</option>
    <option value="19" @(Request["hourto"] == "19" ? " selected=\"selected\"" : "") >19</option>
    <option value="20" @(Request["hourto"] == "20" ? " selected=\"selected\"" : "") >20</option>
    <option value="21" @(Request["hourto"] == "21" ? " selected=\"selected\"" : "") >21</option>
    <option value="22" @(Request["hourto"] == "22" ? " selected=\"selected\"" : "") >22</option>
    <option value="23" @(Request["hourto"] == "23" ? " selected=\"selected\"" : "") >23</option>
    </select>
4

4 に答える 4

0

これを行う方法の例を次に示します。

var hourFrom = document.getElementsByName("hourfrom")[0];
var hourTo = document.getElementsByName("hourto")[0];

hourFrom.onchange = function(){
    var from = parseInt(hourFrom.value, 10),
        toOptions = hourTo.options,
        toOptionsLength = toOptions.length;

    while(toOptionsLength--){
        if(parseInt(toOptions[toOptionsLength].value, 10) < from ){
            toOptions[toOptionsLength].disabled = true;
        }
        else{
            toOptions[toOptionsLength].disabled = false;
        }
    }
};

基本的に、hourTo の hourFrom の前にあるすべてのオプションを無効にしています。

于 2013-04-12T12:32:19.353 に答える
0

私はあなたの要件に従ってそれを更新するだけの例を提供しました........

    <script>
    function check(hourto)
    {
        from=document.getElementById("hourfrom").value;
        to=hourto.value;
        if(to>from)
        {
            alert("From should be less then false");
            return false;
        }
        else
        {
            return true;
        }    
    }
</script>
From:
    <select name="hourfrom" id ="hourfrom">
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
To:
<select name="hourto" id ="hourto" onchange="check(this)">
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
于 2013-04-12T12:34:08.870 に答える