Javascript 禁用按钮提交,直到选择dropdownlist
在这里,我有下拉列表和按钮提交。如何在用户选择dropdownlist之前禁用按钮submit?如果用户选择了dropdownlist中的一个,则该按钮可以单击。下面是我的代码 下拉列表Javascript 禁用按钮提交,直到选择dropdownlist,javascript,html,dropdown,Javascript,Html,Dropdown,在这里,我有下拉列表和按钮提交。如何在用户选择dropdownlist之前禁用按钮submit?如果用户选择了dropdownlist中的一个,则该按钮可以单击。下面是我的代码 下拉列表 validate(); $('input,select').change(validate); $("#dropdown").kendoDropDownList({ optionLabel: "- Select Position -", dataTextField: "functionName"
validate();
$('input,select').change(validate);
$("#dropdown").kendoDropDownList({
optionLabel: "- Select Position -",
dataTextField: "functionName",
dataValueField: "hrsPositionID",
dataSource: {
transport:{
read: {
url: "./testjson.php",
type: "POST",
data: function() {
return {
method: "getDropdown",
}
}
},
},
},
change: function(e){
console.log(this.value());
// $('#AccountingTree').data('kendoTreeView').homogeneous.read();
homogeneous1.read();
homogeneous2.read();
homogeneous3.read();
homogeneous4.read();
homogeneous5.read();
homogeneous6.read();
homogeneous7.read();
homogeneous8.read();
homogeneous9.read();
homogeneous10.read();
homogeneous11.read();
homogeneous12.read();
homogeneous13.read();
homogeneous14.read();
}
}).data('kendoDropDownList');
dropdownlist = $("#dropdown").data("kendoDropDownList");
//AJAX call for button
$("#primaryTextButton").kendoButton();
var button = $("#primaryTextButton").data("kendoButton");
button.bind("click", function(e) {
var test = $("#dropdown").val()
$.ajax({
url: "../DesignationProgramTemplate/testing.php",
type: "post",
data: {'id':test,'progid':array},
success: function (respond) {
// you will get response from your php page (what you echo or print)
//kendo.alert('Success'); // alert notification
if(respond === "SUCCESS")
{
kendo.alert("Data saved");
}else
{
kendo.confirm("Update the data?")
.done(function(){
$.ajax({
type: "POST",
url: "del.php",
data: {'id':test,'progid':array},
success: function(){
kendo.alert("Data updated");
}
});
});
}
},
});
});
function validate(e){
var validation = true;
$('input , select').each(function(){
($(this).val().length>0)
{ validation = false;}
});
if ( validation ) {
$('#primaryTextButton').prop('disabled', true);
} else {
$('#primaryTextButton').prop('disabled', false);
}
}
HTML按钮提交
<button id="primaryTextButton" type="button" value="submit" class="k-primary" style="float:right; padding: 5px 20px; border-radius: 4px;">Submit</button>
我在javaScript中使用了函数validate(e),但没有使用函数。我的代码有任何修改吗?或者其他解决方法?如果您已经在使用jquery,为什么不将操作侦听器绑定到dropdownlist,获取dropdownlist的值并检查是否选中了它。但首先禁用按钮,然后如果dropdownlist更改使按钮启用,如下所示:
$("#primaryTextButton").prop('disabled', true);
$("#dropdown").on("change", function(){
$("#primaryTextButton").prop('disabled', false);
})
您可以再查看一点,例如,检查所选项目是否恢复为默认值,并再次禁用按钮。
希望这有助于您使用以下逻辑:
const$dropdown=$(“#dropdown”)
常量$primaryTextButton=$(“#primaryTextButton”)
$dropdown.on('change',function()){
$primaryTextButton.prop('disabled',false)
})
选择选项
福
酒吧
此处提交
:($(this.val().length>0)
应该在之前提交,如果
不起作用,那么所有这些代码实际上与问题相关吗?请将代码减少到最小数量以复制问题。看。好吧,注意到@JonPI猜测上面的代码对我有用。但我怎么才能更像你说的呢?如果你不介意的话,你能给我看看吗?就像我说的,如果它恢复到默认值,你可以检查它的值。因此,如果在onchange
函数中添加,则可以检查dropdownlist的值是否恢复为default@Hikari请接受答案,如果它解决了原来的问题好的,我会试试。非常感谢。