C# 如果文本框为空,则禁用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16361741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling a button if textbox is empty
提问by user2347308
Hey guys i have a quick and easy question i guess for you. I want to find out if i can create a real time event that would check for value of a textbox. What i mean is if the textbox is empty the button wouldnt be clickable. Im thinking is there anyway apart from validation to do this in code? So far im stuck here:
嘿伙计们,我有一个快速而简单的问题,我想对你来说。我想知道是否可以创建一个实时事件来检查文本框的值。我的意思是,如果文本框为空,则按钮将无法点击。我想除了验证之外还有其他方法可以在代码中执行此操作吗?到目前为止,我被困在这里:
if (YourName.Text = null)
{
SubmitButton.Enabled = "False";
}
Where Yourname is the textbox and SubmitButton is the button :)
其中你的名字是文本框,SubmitButton 是按钮:)
回答by dasblinkenlight
System.Stringprovides a pair of convenient functions called IsNullOrEmptyand IsNullOrWhiteSpacewhich you can use for testing for all kinds of strings that look empty to end users:
System.String提供了一对便利函数调用IsNullOrEmpty和IsNullOrWhiteSpace您可以用来测试各种字符串,看空到最终用户的:
if (string.IsNullOrWhiteSpace(YourName.Text)) {
SubmitButton.Enabled = false; // <<== No double-quotes around false
} else {
// Don't forget to re-enable the button
SubmitButton.Enabled = true;
}
This would disable the button even for string composed entirely of blank characters, which makes sense when you validate a name.
即使对于完全由空白字符组成的字符串,这也会禁用按钮,这在您验证名称时很有意义。
The above is identical to
以上等同于
SubmitButton.Enabled = !string.IsNullOrWhiteSpace(YourName.Text);
which shorter than a version with an if.
它比带有if.
回答by Paul Anderson
SubmitButton.Enabled = YourName.Text.Length > 0;
回答by Aniket Inge
Well, start with the button disabled. Add TextChanged event to the textbox. And each time, check if the string is empty or not (use @dasblinkenlight's code in the TextChanged event handler.
好吧,从禁用按钮开始。将 TextChanged 事件添加到文本框。并且每次检查字符串是否为空(在 TextChanged 事件处理程序中使用 @dasblinkenlight 的代码。
回答by Phil
Here's an old school javascript approach. You could use jquery but if this is all you want to do that would be overkill. In the code below, the oninputevent fires when text is changed either via the keyboard or mouse/tap (you need to capture all, this should do that for you). The javascript then gets the contents of the textbox, removes preceeding and trailing whitespace, and sets the disabled attribute on the submit button equal to the textbox being empty, qualified with .length == 0.
这是一个老派的 javascript 方法。您可以使用 jquery,但如果这就是您想要做的,那将是矫枉过正。在下面的代码中,oninput当通过键盘或鼠标/点击更改文本时会触发该事件(您需要捕获所有内容,这应该为您完成)。然后 javascript 获取文本框的内容,删除前后空格,并将提交按钮上的 disabled 属性设置为等于文本框为空,以.length == 0.
Note: if you do not want to disable for text that is only whitespace change submit.disabled = (tb.value.trim().length == 0);to submit.disabled = (tb.value.length == 0);which removes the call to the trim() method. The trim() method strips the preceeding and trailing whitespace.
注意:如果你不希望禁用的文本只是空白的变化submit.disabled = (tb.value.trim().length == 0);,以submit.disabled = (tb.value.length == 0);用于删除调用TRIM()方法。trim() 方法去除前面和后面的空格。
This approach removes the need for a postback to enable or disable the submit button.
这种方法不需要回发来启用或禁用提交按钮。
<!doctype html>
<!-- tested in firefox -->
<html>
<head>
<title>Demo</title>
<script type="text/javascript">
var enableInput = function() {
var tb = document.getElementById("text_box");
var submit = document.getElementById("submit");
submit.disabled = (tb.value.trim().length == 0);
};
</script>
</head>
<body>
<form>
<input type="text" id="text_box" name="test" oninput="enableInput();"></input><br/>
<input id="submit" type="submit"></input>
</form>
</body>
</html>

