javascript 根据 TextBox.Text 文本更改启用或禁用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8163929/
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
Enable or Disable Button Based On TextBox.Text Text Changes
提问by rofans91
I am creating a web application for my company. My application has a button and a textbox.
我正在为我的公司创建一个 Web 应用程序。我的应用程序有一个按钮和一个文本框。
What I want to do is entering some value into the textbox and then when i click the button the application will process the value based on the input in the textbox.
我想要做的是在文本框中输入一些值,然后当我单击按钮时,应用程序将根据文本框中的输入处理该值。
Now here is the tricky part.
现在是棘手的部分。
After the button is clicked once and the text in textbox remains, the button shall disappear.
单击一次按钮后,文本框中的文本仍然存在,按钮将消失。
However if there is modification in the textbox.text, the button shall reappear.
但是如果textbox.text 有修改,按钮会重新出现。
But if the textbox.tex somehow return to original value, the button shall disappear again.
但是如果 textbox.tex 以某种方式返回到原始值,按钮将再次消失。
Please its quite urgent, I have tried my best to do it already, yet so far unsuccessful. I also did a lot of research in Google, but so far none of my findings suit my case.
拜托了很紧急,我已经尽力去做了,但到目前为止没有成功。我也在谷歌做了很多研究,但到目前为止,我的发现都不适合我的情况。
Your help is appreciated.
感谢您的帮助。
回答by Moe Sweet
You got it here http://jsfiddle.net/ywe9G/
你在这里得到它http://jsfiddle.net/ywe9G/
var ori = '';
$(document).ready(function(){
$('button').hide();
$('input').keyup(function(){
if($(this).val() != ori){
$('button').show();
}else{
$('button').hide();
}
});
$('button').click(function(){
ori = $('input').val();
$(this).hide();
});
});
回答by masterchris_99
private string oldTextboxValue = "";
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != oldTextboxValue)
{
button1.Visible = true;
}
else
{
button1.Visible = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
oldTextboxValue = textBox1.Text;
button1.Visible = false;
}
回答by Vivek
to disable button you can do this-
要禁用按钮,您可以这样做-
var oldvalue="";
$('#buttonid').click(function(){
//your code
oldvalue = $("#textBox").text();
this.hide();
}
$("#textBoxId").keypress(function(){
if(this.value!=oldvalue){
this.show();
}
}
回答by anupam
HTML
HTML
<textarea id='mytext' rows='4' cols='20'></textarea>
<br>
<input type="button" id='my_button' value="click">
<input type="hidden" id="my_hidden">
jQuery
jQuery
var clicked=false;
jQuery(document).ready(function() {
$("#my_button").click(function(){
$("#my_hidden").val($("#mytext").val());
$(this).hide();
clicked=true;
});
$("#mytext").keyup(function(){
if(clicked==true && $(this).val()==$("#my_hidden").val()) {
$("#my_button").hide();
}else if(clicked==true){
$("#my_button").show();
}
});
});
You can similarly enable/disable button instead of show/hide
您可以类似地启用/禁用按钮而不是显示/隐藏