简体   繁体   English

JavaScript将所选值保留在选项中

[英]JavaScript Keep selected value in option

I have two selects, the "section" option is disabled until the "grade" has a value of 7 to 10 我有两个选择,“部分”选项被禁用,直到“等级”的值为7到10

<div class="form-inline">
            <label for="studgrade">Grade</label>
            <select class="form-control" onchange="changeGrade();" id="studgrade" name="studgrade">
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
            </select>
            <label for="studsection">Section</label>
            <select class="form-control" id="studsection" name="studsection">
                <option>7</option>
                <option>8</option>
                <option>9</option>
                <option>10</option>
            </select>
          </div>

But everytime I click 7, the "grade" value returns to 6, which keeps the "section" option disabled. 但是,每当我单击7时,“成绩”值将返回到6,这会使“部分”选项保持禁用状态。

Here is the script: 这是脚本:

<script>
        $(document).ready(function(){

            var selectedgrade = document.getElementById("studgrade");
            var newgrade = selectedgrade.options[selectedgrade.selectedIndex].value;
            document.getElementById("studsection").disabled = true;

            if(newgrade < 7 || newgrade > 10){
                   document.getElementById("studsection").disabled = true;
               } else if(newgrade >= 7 && newgrade <= 10) {
                   document.getElementById("studsection").disabled = false;
               }

        });

        </script>
        <script type="text/javascript">

           function changeGrade() {

                //var selectedgrade = $grade.options[$grade.selectedIndex].value;
                var selectedgrade = document.getElementById("studgrade");
                document.getElementById("studgrade").value = selectedgrade;
                window.location.href = "<?php echo base_url().'schoolportal/addstudent'?>";

           }

        </script>

我自己弄清楚了,我试图将条件语句放在方法changeGrade()中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM