猿问

如何在下拉菜单中动态选择选项?

选了一个科目然后根据选的科目出现时间怎么办?每个科目有多个时间选项以及选择科目时如何显示和时间显示所选科目的时间


<div class="form-group">

    <label class="control-label"> Subject 1: </label> <font color="red"> * </font>

        <select id="levelSbj1" name="levelSbj1" class="form-control" required="required">

            <option value="0"> - Select Subject--</option>

            <option value="1"> Bahasa Malaysia</option>

            <option value="2"> English</option>

            <option value="3"> Mathematics</option>

            <option value="4"> Science</option>

        </select>

</div>

            

<div class="form-group">

    <label class="control-label"> Time and Day: </label> <font color="red"> * </font>

        <select id="levelLvl1" name="levelLvl1" class="form-control" required="required">

            <option value="0"> - Select Time and Day--</option>

            <option value="1"> 10.00 - 12.00 am (Saturday)</option>

            <option value="1"> 2.00 - 4.00 pm (Saturday)</option>

            <option value="2"> 9.00am - 11.00am (Friday)</option>

            <option value="2"> 3.00 - 5.00 pm (Friday)</option>

            <option value="3"> 10.00 - 12.00 am (monday)</option>

            <option value="3"> 2.00 - 4.00 pm (tuesday)</option>

            <option value="4"> 9.00 - 11.00 am (thursday)</option>

            <option value="4"> 3.00 - 5.00 pm (sunday)</option>

        </select>

</div>

科目一:* - 选择科目--马来文英文数学科学

            


时间和日期:* - 选择时间和日期-- 10.00 - 12.00 am(星期六)2.00 - 4.00 pm(星期六)9.00am - 11.00am(星期五)3.00 - 5.00 pm(星期五)10.00 - (12.00 am)下午 4 点(星期二)上午 9 点至 11 点(星期四)下午 3 点至 5 点(星期日)     


开心每一天1111
浏览 148回答 1
1回答

HUX布斯

在客户端,您可以像这样尝试使用 jQuery:$(document).ready(function() {&nbsp; // when selection changes on Subject&nbsp; $('#levelSbj1').on('change', function(e) {&nbsp; &nbsp; var currentSubject = $(this);&nbsp; &nbsp; var timeSelect = $('#levelLvl1');&nbsp; &nbsp; // select time which corresponds to the subbject's value&nbsp; &nbsp; timeSelect.val(currentSubject.val());&nbsp; &nbsp; var resultsDiv = $('#result');&nbsp; &nbsp; switch (currentSubject.val()) {&nbsp; &nbsp; &nbsp; case '1': // Bahasa Malaysia&nbsp; &nbsp; &nbsp; &nbsp; resultsDiv.html('<span>Subject: ' + currentSubject.find("option:selected").text() + ', Time: ' + timeSelect.find('option').eq(1).text() + ' - ' + timeSelect.find('option').eq(2).text() + '</span>');&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case '2': // English&nbsp; &nbsp; &nbsp; &nbsp; resultsDiv.html('<span>Subject: ' + currentSubject.find("option:selected").text() + ', Time: ' + timeSelect.find('option').eq(1).text() + ' - ' + timeSelect.find('option').eq(3).text() + '</span>');&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case '3': // Mathematics&nbsp; &nbsp; &nbsp; &nbsp; resultsDiv.html('<span>Subject: ' + currentSubject.find("option:selected").text() + ', Time: ' + timeSelect.find('option').eq(3).text() + ' - ' + timeSelect.find('option').eq(4).text() + '</span>');&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case '4': // Science&nbsp; &nbsp; &nbsp; &nbsp; resultsDiv.html('<span>Subject: ' + currentSubject.find("option:selected").text() + ', Time: ' + timeSelect.find('option').eq(2).text() + ' - ' + timeSelect.find('option').eq(3).text() + '</span>');&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; })})<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"><script src="https://code.jquery.com/jquery-3.3.1.min.js"></script><div class="form-group">&nbsp; <label class="control-label">Subject 1:</label>&nbsp; <font color="red">*</font>&nbsp; <select id="levelSbj1" name="levelSbj1" class="form-control" required="required">&nbsp; &nbsp; <option value="0">--Select Subject--</option>&nbsp; &nbsp; <option value="1">Bahasa Malaysia</option>&nbsp; &nbsp; <option value="2">English</option>&nbsp; &nbsp; <option value="3">Mathematics</option>&nbsp; &nbsp; <option value="4">Science</option>&nbsp; </select></div><div class="form-group">&nbsp; <label class="control-label">Time and Day:</label>&nbsp; <font color="red">*</font>&nbsp; <select id="levelLvl1" name="levelLvl1" class="form-control" required="required">&nbsp; &nbsp; <option value="0">--Select Time and Day--</option>&nbsp; &nbsp; <option value="1">10.00 - 12.00 am (Saturday)</option>&nbsp; &nbsp; <option value="2">2.00 - 4. 00 pm (Saturday)</option>&nbsp; &nbsp; <option value="3">9.00 - 11.00 am (Friday)</option>&nbsp; &nbsp; <option value="4">3.00 - 5. 00 pm (Friday)</option>&nbsp; &nbsp; <option value="5">10.00 - 12.00 am (monday)</option>&nbsp; &nbsp; <option value="6">2.00 - 4. 00 pm (tuesday)</option>&nbsp; &nbsp; <option value="7">9.00 - 11.00 am (thursday)</option>&nbsp; &nbsp; <option value="8">3.00 - 5. 00 pm (sunday)</option>&nbsp; </select></div><div id="result"></div>更新您不能在 HTML 选择中显示多个选定选项,除非使用这样的多选:在此处输入链接描述但是,您可以提取多个选项的值并将它们显示在某处。我已经更新了代码片段。检查它,看看它是如何完成的。这只是一个虚拟示例,您需要根据自己的要求进行调整。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答