1 回答
TA贡献1865条经验 获得超7个赞
您可以使用for-loop
特定值禁用所有选项,该值将传递给一个函数,在该函数中我们将获取length
选择框中的选项,然后使用此循环来禁用选择框中的选项。
演示代码:
function proses_stok() {
var produk_stock = document.getElementById("produk_size").value;
document.getElementById("produk_stok").value = produk_stock;
//if value is small
if (produk_stock == "stok_s") {
disable_values(15); //disable from
} else if (produk_stock == "stok_m") {
disable_values(20); //disable from
} else {
//enable all options
$("#produk_stok option").prop('disabled', false);
}
}
function disable_values(start) {
var s = document.getElementById("produk_stok");
var end = s.options.length; //getting length of option
//first enabled all options
$("#produk_stok option").prop('disabled', false);
//loop through all options
for (var i = start; i < end; i++) {
s.options[i].disabled = true; //disabled
}
}
<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%>
<option value=0>Select Size:</option>
<option value=stok_s>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%">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<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>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
</select>
更新1:
要添加选项直到特定值,您可以使用for循环将end值传递给它,然后将数据附加到某个变量,然后使用innerHTML在选择框下显示来附加此数据。
演示代码:
function proses_stok() {
var produk_stock = document.getElementById("produk_size").value;
//if value is small
if (produk_stock == "stok_s") {
disable_values(15); //add till this option
} else if (produk_stock == "stok_m") {
disable_values(20); //add till this option
} else if(produk_stock == "0") {
document.getElementById("produk_stok").innerHTML = "<option value='0'>0</option>";
}else{
//if large size select
//do somthing ..
}
}
function disable_values(end) {
var s = document.getElementById("produk_stok");
var data="";
//loop through all options
for (var i = 1; i <= end; i++) {
//append options
data +="<option value="+i+">"+i+"</option>";
}
//add data to select box
s.innerHTML= data;
}
<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%>
<option value=0>Select Size:</option>
<option value=stok_s>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%">
<option value="0">0</option>
</select>
- 1 回答
- 0 关注
- 121 浏览
添加回答
举报