我有一个 php 循环,可以回显多个 <select>
作为在线订购系统一部分的元素,用户可以在其中选择特定商品的数量。
执行表单的按钮运行一个 php 脚本,将值插入到我的数据库中,但是,我需要能够忽略等于“0”的值。
通过在提交按钮上创建 onClick 事件(JS 函数),我执行以下代码:
$('#quantityForm').find(':select').each(function(){
if($(this).val() == "0"){
$(this).attr('disabled', 'disabled');
}
})
除了设置<select>
的代码之外,一切似乎都工作正常。元素被禁用,因为我的 php 脚本尝试获取所有“0”值。
我是不是做错了什么?
附加代码:
<form id="quantityForm" method="get">
<?php get_items($example); ?>
<button onClick="checkQuantity()" id='basket' type='submit' name='submit'><i class='fas fa-shopping-basket'></i></button>
</form>
最佳答案
$('#quantityForm')
//find all the select elements in the form
.find('select')
//filter and return only those that have a value of 0
.filter(function(){
return this.value === '0';
})
//make them disabled
.prop('disabled', true);
关于javascript - 将选择元素设置为在表单提交时禁用(JQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50819335/