如何创建自动禁用选择选项?

我对选项选择有一些问题。

如果用户没有选择 T 恤尺寸,我希望选择选项自动禁用。在总订单中,选项选择与库存尺寸 T 恤相同的最大订单。

考试:尺码 S 有 15 个库存 尺码 M 有 20 个库存

并在选项中选择与 T 恤尺码库存相对应的最大订单。如果用户选择尺寸 s 选项选择订单自动最大数量为 15,并且用户选择尺寸 m 选项选择订单自动最大数量为 20。

http://img1.mukewang.com/649fee5400015b5303860179.jpg

关于从数据库中获取的库存数量


我将 codeigniter 与 bootstrap 一起使用


这是我的代码:选择选项尺码 T 恤


<?php


$sprod = $stok['stok_s'];

$mprod = $stok['stok_m'];

$lprod = $stok['stok_l'];


if ($sprod == 0 && $mprod != 0 && $lprod != 0) {

    echo '<select name=produk_size id=produk_size class=form-control onchange=proses_stok() style=width:95%>

    <option value=0>Select Size:</option>

    <option value=stok_s disabled>s</option>

    <option value=stok_m>m</option>

    <option value=stok_l>L</option>

    </select>';

    ?>

选项 选择订单号


<select name="produk_stok" id="produk_stok" class="form-control" style="width:95%">

    <?php


    $stok_prod = $stok['stok_s'] + $stok['stok_m'] + $stok['stok_l'];


    if ($stok_prod == 0 )

    {

        echo "<option value=0> 0 </option>";

    }

    else{


        for($a=$stok['min_buy'];

            $a<=$stok['stok']; $a+=1)

        { 

          echo "<option value=$a>$a</option>";

      }

  }

  ?>

</select> 

JavaScript


<script type="text/javascript">

    function proses_stok(){

        var produk_stock = document.getElementById("produk_size").value;

        document.getElementById("produk_stock").value=produk_stock;

    }

</script>

当我更改大小时,选项选择编号没有更改。当我选择“选择尺码”时,选择编号订单选项并未禁用/只有 0 个库存


宝慕林4294392
浏览 122回答 1
1回答

鸿蒙传说

您可以使用for-loop特定值禁用所有选项,该值将传递给一个函数,在该函数中我们将获取length选择框中的选项,然后使用此循环来禁用选择框中的选项。演示代码:function proses_stok() {&nbsp; var produk_stock = document.getElementById("produk_size").value;&nbsp; document.getElementById("produk_stok").value = produk_stock;&nbsp; //if value is small&nbsp; if (produk_stock == "stok_s") {&nbsp; &nbsp; disable_values(15); //disable from&nbsp; } else if (produk_stock == "stok_m") {&nbsp; &nbsp; disable_values(20); //disable from&nbsp; } else {&nbsp; &nbsp; //enable all options&nbsp; &nbsp; $("#produk_stok option").prop('disabled', false);&nbsp; }}function disable_values(start) {&nbsp; var s = document.getElementById("produk_stok");&nbsp; var end = s.options.length; //getting length of option&nbsp; //first enabled all options&nbsp; $("#produk_stok option").prop('disabled', false);&nbsp; //loop through all options&nbsp; for (var i = start; i < end; i++) {&nbsp; &nbsp; s.options[i].disabled = true; //disabled&nbsp;&nbsp; }}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select name=produk_size id=produk_size class=form-control onchange=proses_stok() style=width:95%>&nbsp; <option value=0>Select Size:</option>&nbsp; <option value=stok_s>s</option>&nbsp; <option value=stok_m>m</option>&nbsp; <option value=stok_l>L</option></select><select name="produk_stok" id="produk_stok" class="form-control" style="width:95%">&nbsp; <option value="1">1</option>&nbsp; <option value="2">2</option>&nbsp; <option value="3">3</option>&nbsp; <option value="4">4</option>&nbsp; <option value="5">5</option>&nbsp; <option value="6">6</option>&nbsp; <option value="7">7</option>&nbsp; <option value="8">8</option>&nbsp; <option value="9">9</option>&nbsp; <option value="10">10</option>&nbsp; <option value="11">11</option>&nbsp; <option value="12">12</option>&nbsp; <option value="13">13</option>&nbsp; <option value="14">14</option>&nbsp; <option value="15">15</option>&nbsp; <option value="16">16</option>&nbsp; <option value="17">17</option>&nbsp; <option value="18">18</option>&nbsp; <option value="19">19</option>&nbsp; <option value="20">20</option>&nbsp; <option value="21">21</option>&nbsp; <option value="22">22</option>&nbsp; <option value="23">23</option>&nbsp; <option value="24">24</option>&nbsp; <option value="25">25</option></select>更新1:要添加选项直到特定值,您可以使用for循环将end值传递给它,然后将数据附加到某个变量,然后使用innerHTML在选择框下显示来附加此数据。演示代码:function proses_stok() {&nbsp; var produk_stock = document.getElementById("produk_size").value;&nbsp; //if value is small&nbsp; if (produk_stock == "stok_s") {&nbsp; &nbsp; disable_values(15); //add till this option&nbsp; } else if (produk_stock == "stok_m") {&nbsp; &nbsp; disable_values(20); //add till this option&nbsp; } else if(produk_stock == "0") {&nbsp; &nbsp; document.getElementById("produk_stok").innerHTML = "<option value='0'>0</option>";&nbsp; }else{&nbsp; //if large size select&nbsp; //do somthing ..&nbsp; }}function disable_values(end) {&nbsp; var s = document.getElementById("produk_stok");&nbsp;var data="";&nbsp; //loop through all options&nbsp; for (var i = 1; i <= end; i++) {&nbsp; //append options&nbsp; data +="<option value="+i+">"+i+"</option>";&nbsp; }&nbsp; //add data to select box&nbsp;s.innerHTML= data;&nbsp;&nbsp;}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select name=produk_size id=produk_size class=form-control onchange=proses_stok() style=width:95%>&nbsp; <option value=0>Select Size:</option>&nbsp; <option value=stok_s>s</option>&nbsp; <option value=stok_m>m</option>&nbsp; <option value=stok_l>L</option></select><select name="produk_stok" id="produk_stok" class="form-control" style="width:95%">&nbsp;<option value="0">0</option></select>
打开App,查看更多内容
随时随地看视频慕课网APP