javascript - JQuery 如果某些文本框为空,则禁用按钮

标签 javascript jquery .net

我见过很多人在文本框为空时禁用按钮的例子,但我还没有发现任何只会禁用某些文本框的按钮的例子。我是 Jquery 的新手,我知道它是伪编码的,但你可以理解。我必须调用哪个 Jquery 函数以便它不断检查?以及如何在 if 子句中使用 or 语句来确定任何文本框字段是否为空?

if( $('#txtEvent').val.length === 0 || $("#txtID").val.length === 0)
 {
  $('#btnSave').attr("disabled", "disabled");
 }
else 
 {
  $('#btnSave').attr("enabled", "enabled");
 }

表单控件

 <asp:TextBox ID="txtEvent" runat="server"></asp:TextBox>
< asp:TextBox ID="txtID" runat="server"></asp:TextBox>
<asp:Button ID="btnSave" runat="server"" Text="Save and Next" />

最佳答案

您可以通过两种不同的方式进行操作:

if (!$("#txtEvent").val()) { //undefined will yield false
//call a method! .val() not .val
    $("#btnSave").attr("disabled", "disabled");
} else {
    $("#btnSave").attr("enabled", "enabled");
}

或者:

if ($("#txtEvent").length > 0) {
    $("#btnSave").attr("disabled", "disabled");
} else {
    $("#btnSave").attr("enabled", "enabled");
}

如果你想让它们持续运行,将它们包装在:

$("#txtEvent").on("change", function() { //code });
//using the onchange event will trigger the code whenever the txtbox changes.
//you can also use onblur if you want it to trigger AFTER the txtbox loses focus

请注意,您必须将它们转换为正确的 asp 代码!这只是一个后勤答案。

关于javascript - JQuery 如果某些文本框为空,则禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18478336/

相关文章:

.net - 在.net core中更新具有身份的用户角色而无需重新登录

.net - 如何从同一个 EXE 应用程序引用 x86 和任何 CPU 程序集?

javascript - 迭代有效 JSON 时无法将 undefined 或 null 转换为对象

javascript - 如何将文本框动态添加到单击编辑的文本部分?

Jquery 插件 filthypillow。如何将时间格式设置为 24 小时制

带有中通配符的 jQuery 复杂选择器?

C#/.NET 不是通用 LinkedList 存在吗?

javascript - Haml form_tag 正在破坏 javascript

javascript - ckeditor 将选择转换为指定的标签

javascript - 超时后 jQuery 动画到底部功能