[英]Combine onclick and onkeypress
我有这个html表单,它实际上不会将数据发送到php文件,但是会将数据发送到javascipt函数。
如果我点击按钮工作正常,但是当我点击输入时没有任何反应。
如果用鼠标点击或者用户点击进入,我希望按钮工作。
我该如何解决? 谢谢
<input type="button" value="Reveal Now" style="margin: 3px;font-family: 'festivo_lcbasic', Arial, Helvetica, sans-serif; text-transform:uppercase; font-size:26px; text-decoration:none; font-weight:normal; padding-top: 10px;" onclick="check(this.form.Pass.value)" onkeypress="check(this.form.Pass.value)"/>
</form>
<script type="text/javascript">
var password = ["#123, #345"];
function check(pass) {
for(a = 0; a < password.length; a++) {
if (pass == password[a]) {
top.location.href="enter.html";
return;
}
}
location.href="incorrect.html";
}
</script>
当您在text
字段中键入用户输入enter-key
时,我认为您正在尝试检查某些text
。 我假设你的input text
是这样的
<input type="text" id="Pass" />
至
<input type="text" id="Pass" onkeypress="runScript(event)" />
并在script
添加此功能
function runScript(e) {
if (e.keyCode == 13) {
var pas = document.getElementById("Pass").value;
check(pas) //calling your button function
}
}
完整的脚本:
<script type="text/javascript">
var password = ["#123, #345"];
function check(pass) {
for(a = 0; a < password.length; a++) {
if (pass == password[a]) {
top.location.href="enter.html";
return;
}
}
location.href="incorrect.html";
}
function runScript(e) {
if (e.keyCode == 13) {
var pas = document.getElementById("Pass").value;
check(pas) //calling your button function
}
}
</script>
要么:
<input type="text" id="Pass" onkeypress="runScript(event, this.value)" />
脚本:
function runScript(e, pas) {
if (e.keyCode == 13) {
check(pas) //calling your button function
}
}
你的代码很糟糕,原因很多,我现在还没有进入,但如果你查看你的javascript控制台(chrome / firefox),你会看到这个错误:
以下是我修复代码的方法。 1.将ID“myButton”添加到2.将“myButton”传递给check()函数3.使用getElementById()从函数中获取
<input id="myButton" type="button" value="Reveal Now" style="margin: 3px;font-family: 'festivo_lcbasic', Arial, Helvetica, sans-serif; text-transform:uppercase; font-size:26px; text-decoration:none; font-weight:normal; padding-top: 10px;" onclick="check('myButton')" onkeypress="check('myButton')"/>
<script type="text/javascript">
var password = ["#123, #345"];
function check(someID) {
for(a = 0; a < password.length; a++) {
if (document.getElementById(someID).value == password[a]) {
top.location.href="enter.html";
return;
}
}
location.href="incorrect.html";
}
</script>
说得通?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.