gpt4 book ai didi

javascript - 如果下拉列表中没有选定值,则禁用按钮

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:25 24 4
gpt4 key购买 nike

我有一个代码,它在页面加载时禁用按钮,因为下拉列表的值为空。 However, when a value is selected (the values are from the database, it is populated and it is working), the button is still disabled.

J查询:

<script>
$(document).ready(function(){
$('.send').attr('disabled',true);

$('#kagawad').keyup(function(){
if($(this).val() != ""){
$('.send').attr('disabled', false);
}
else
{
$('.send').attr('disabled', true);
}
})
});
</script>

html:

<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Select Kagawad</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<?php
include 'config.php';
$selectSql = "SELECT firstName, middleName, lastName
FROM table_position p
LEFT JOIN person r ON p.Person_idPerson = r.idPerson
WHERE p.bar_position = 'Barangay Kagawad' AND p.activeOrInactive = 'Active'";
$result = mysqli_query($conn, $selectSql);
?>

<select class="form-control" id = "kagawad" name = "kagawad" required>
<option value="">Choose...</option>
<?php
while ($line = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $line['firstName'].' '.$line['middleName'].' '.$line['lastName'];?>"> <?php echo $line['firstName'].' '.$line['middleName'].' '.$line['lastName'];?> </option>

<?php
mysqli_close($conn);
}
?>
</select>
</div>
<button id="send" type="submit" class="send btn btn-success" name="addCedula">Save Record</button>

我该怎么做?我需要修改我的代码什么?谢谢!

最佳答案

  1. 使用change <select> 上的事件.
  2. 代替attr() , 使用 prop()设置禁用状态。
  3. 使用 ID 选择器禁用按钮。

代码:

$('#kagawad').on('change', function () {
$('#send').prop('disabled', !$(this).val());
}).trigger('change');

关于javascript - 如果下拉列表中没有选定值,则禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com