JavaScript代码无法在mozilla Firefox中运行? [英] Javascript code not working in mozilla firefox?

查看:78
本文介绍了JavaScript代码无法在mozilla Firefox中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,

我写了一个javascript代码来防止文本框中的空格。它在Internet Explorer中运行良好,但它不适用于mozilla firefox。我附上以下代码供您参考。请给我这个问题的解决方案。





Default3.aspx

****** *************

Hi sir,
I wrote one javascript code to prevent "space" in textbox. Its working fine in Internet explorer, but it is not working in mozilla firefox. I attached the code below for your reference. Kindly give me the solution for this problem.


Default3.aspx
*******************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function AvoidSpace() {
            if (event.keyCode == 32) {
                event.returnValue = false;
                return false;
            }
        }
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:TextBox ID="txtEmpName" runat="server" onkeypress="return AvoidSpace()"></asp:TextBox>
    </div>
    </form>
</body>
</html>

推荐答案

问题

您尚未将该活动传递给该功能。



解决方案

1.您需要执行以下操作。

Problem
You have not passed the event into the function.

Solution
1. You need to do like below.
<asp:TextBox ID="txtEmpName"

             runat="server" 

             onkeypress="return AvoidSpace(event)">
</asp:TextBox>





2.并在函数中使用此事件。修改如下函数。



2. And use this event in the function. Modify the function as below.

function AvoidSpace(event) {
    var k = event ? event.which : window.event.keyCode;
    if (k == 32) return false;
}




Quote:

onkeypress事件并未针对所有人触发所有浏览器中的键类型。有关详细信息,请参阅下表。

要获取按下的键,请使用keyCode,charCode和哪些事件属性。

The onkeypress event is not fired for all key types in all browsers. For details, please see the table below.
To get the pressed key, use the keyCode, charCode and which event properties.

参考 - onkeypress事件|按键事件 [ ^ ]。



window.event.keyCode 适用于Internet Explorer。

事件。哪个适用于其他浏览器。



DEMO

onkeypress事件演示 - 限制空格键 [ ^ ]。

Refer - onkeypress event | keypress event[^].

window.event.keyCode is for Internet Explorer.
event.which is for other browsers.

DEMO
onkeypress Event Demo - restrict space key[^].


您只返回false而且从不返回true。您应该在所有情况下返回一些内容。



-SA
You only return false and never true. You should return something on all cases.

—SA


这篇关于JavaScript代码无法在mozilla Firefox中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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