如何使用javascript检查keypress事件上的文本框上的复选框 [英] how to check the checkbox on textbox on keypress event using javascript

查看:76
本文介绍了如何使用javascript检查keypress事件上的文本框上的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页表单中有一个复选框和一个文本框



这是我的代码

in my web form one checkbox and one textbox

this is my code

protected void txtpwd_TextChanged(object sender, EventArgs e)
       {
           if (txtpwd.Text == "123")
           {
               chckbox.Checked = true;
           }
           else
           {
               chckbox.Checked = false;
           }
       }







它正在工作但是

i想要显示结果,就像我输入密码时一样,它会被更改





但它正在工作

当我离开texbox时它会检查



你可以告诉我



thankxxx




it's working but
i wanna show result like when ever i typing the password it will be changed


but it's working
when ever i leave the texbox it will checked

can u please tell me

thankxxx

推荐答案

如果你想在用户输入时这样做,你就不能用C#来做 - 正如MSDN所说:

当文本框的内容在发布到服务器的帖子之间发生变化时,会引发TextChanged事件。 https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged(v = VS .110).aspx [ ^ ]



这并不意味着它会在用户每次按下字符时发生!

如果你想让它作为用户类型,您需要在JavaScript中执行: http://www.w3schools.com/jsref/event_onchange.asp [ ^ ]
If you want to do that while the user is typing, you can't do it in C# - as MSDN says:
"The TextChanged event is raised when the content of the text box changes between posts to the server." https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged(v=vs.110).aspx[^]

That doesn't mean it happens on each character press by the user!
If you want this to work as the user types, you need to do it in JavaScript: http://www.w3schools.com/jsref/event_onchange.asp[^]


您将需要一种方法来检查密码的有效性。

例如在下面的代码中,IsValidPasxword()将检查密码是否有效。



You will need a method that will check the validity of the password.
For e.g. in code below IsValidPasxword() will check if password is valid.

protected void txtpwd_TextChanged(object sender, EventArgs e)
{
    if (txtpwd.Text == IsValidPasxword())
    {
        chckbox.Checked = true;
    }
    else
    {
        chckbox.Checked = false;
    }
}
private bool IsValidPasxword()
{
     //Check conditions for strong password
}


您想要检测用户输入的每个击键,更好地使用只能在浏览器上进行的JavaScript功能,而无需前往服务器。看看这个: http://www.w3schools.com/jsref/event_onkeyup.asp [ ^ ]
You want to detect every keystroke that a use enters, better use a JavaScript function which will only occur on the browser, without having to make a round trip to the server. Check this out: http://www.w3schools.com/jsref/event_onkeyup.asp[^]


这篇关于如何使用javascript检查keypress事件上的文本框上的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆