如果下拉列表中没有选定的值,请停用按钮 [英] Disable button if no selected value in dropdown
本文介绍了如果下拉列表中没有选定的值,请停用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个代码,因为下拉列表的值为空,所以它禁用页面加载时的按钮。但是,如果选择了一个值(这些值来自数据库,它已填充且正在工作),则该按钮仍处于禁用状态。
Jquery:
< script>
$(document).ready(function(){
$('。send')。attr('disabled',true);
$('#kagawad' ).keyup(function(){
if($(this).val()!=){
$('。send')。attr('disabled',false); $ b'b}
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>选择Kagawad< / label>
< div class =col-md-9 col-sm-9 col-xs-12>
<?php
包含'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);
?>
< option value =>选择...< / 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'];?> < /选项>
<?php
mysqli_close($ conn);
}
?>
< / select>
< / div>
< button id =sendtype =submitclass =send btn btn-successname =addCedula>保存记录< / button>
我该怎么做?我需要修改我的代码?
解决方案
- 使用
更改
事件在< select>
。 - 而不是
attr()
,使用prop()
来设置禁用状态。 - 使用ID选择器禁用按钮。 $ b
代码:
$('#kagawad')。on('change',function(){
$('#send')。prop('disabled',!$(this).val()); $ b $ ('change');
I have a code where it disables the button on page load since the value of the dropdown is empty. However, when a value is selected (the values are from the database, it is populated and it is working), the button is still disabled.
Jquery:
<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>
How can I do it? What do I need to modify my code? Thank you!
解决方案
- Use
change
event on the<select>
. - Instead of
attr()
, useprop()
to set the disabled status. - Use ID selector, to disable the button.
Code:
$('#kagawad').on('change', function () {
$('#send').prop('disabled', !$(this).val());
}).trigger('change');
这篇关于如果下拉列表中没有选定的值,请停用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文