2 回答

TA貢獻1828條經驗 獲得超3個贊
我建議像這樣禁用它:
如果選擇輸入等于“Bags”
選擇最接近的“項目名稱”輸入并將其隱藏
根據你的代碼
let restricts = ["bags", "kg"];
function hideQuantity(e) {
if (restricts.indexOf(e.target.value) > -1) {
e.target.closest("tr").querySelector(".item_quantity").setAttribute("disabled", "disabled");
} else {
e.target.closest("tr").querySelector(".item_quantity").removeAttribute("disabled", "disabled");
}
}
$(document).ready(function () {
$(document).on('click', '.add', function () {
var html = '';
html += '<tr>';
html += '<td><select onclick="hideQuantity(event)" name="item_unit[]" class="form-control item_unit">';
html += '<option value="">Select Unit</option><option value="bags">Bags</option><option value="inch">Inch</option><option value="kg">Kg</option><?php echo numopt(); ?>';
html += '</select></td>';
html += '<td><input type="text" name="item_name[]"" class="form-control item_name" /></td>';
html += '<td><input type="text" name="item_quantity[]"" class="form-control item_quantity" /></td>';
html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
$('#item_table').append(html);
});
...
更多解釋:
首先,我添加了一些靜態數據用于測試:
html += '<option value="">Select Unit</option><option value="bags">Bags</option><option value="inch">Inch</option><option value="kg">Kg</option><?php echo numopt(); ?>';
其次,創建一個restricts數組。item_unit該數組保存您想要隱藏輸入的位置的值item_quantity:
let restricts = ["bags", "kg"]; // add as many as you want
第三,我創建了函數,該函數的目的是如果選擇的輸入值與數組的任何其他值匹配,hideQuantity則隱藏item_quantity輸入:item_unitrestricts
function hideQuantity(e) {
if (restricts.indexOf(e.target.value) > -1) {
e.target.closest("tr").querySelector(".item_quantity").setAttribute("disabled", "disabled");
} else {
e.target.closest("tr").querySelector(".item_quantity").removeAttribute("disabled", "disabled");
}
}
最后,hideQuantity為每個item_unit選擇輸入添加函數:
html += '<td><select onclick="hideQuantity(event)" name="item_unit[]" class="form-control item_unit">';
添加回答
舉報