以下JavaScript似乎在Firefox中有效,但在Chrome上却没有?

时间:2012-10-22 14:10:52

标签: javascript xhtml

我是网页设计/开发的新手;我有以下代码来检查是否在文本框中输入了数据。 JavaScript会检查是否添加了数据,如果没有输入则会出现错误?

此脚本适用于FireFox,但不适用于Google Chrome。错误消息不会出现,数据会直接添加到数据库中。

<script type="text/javascript">      
        function validate(){
            if(
            document.vulcanoForm.device_name.value == "" ||
            document.vulcanoForm.muid.value == "" ||
            document.vulcanoForm.map_version.value == "0.000")
            {
                alert("Please fill out all fields before clicking submit!");
                return false;  
            }
            else
            {
                alert("Hold on just adding to database :)");
                return true;  
            }
        }
    </script>

<body>
    <a href="index.xhtml"> Mogadishu </a> <br/>
    <form action="vulcanoprocess.php" method="post" onsubmit="return validate()" name="vulcanoForm">

        <fieldset>
            <legend>Device Information</legend>

            <label for="device_name">Device Name:</label><br />
            <input type="text" name="device_name" id="device_name" /><br />

            <label for="muid">MUID:</label><br />
            <input type="text" name="muid" id="muid" /><br />

            </form>
</body>

2 个答案:

答案 0 :(得分:0)

Onsubmit doesn't work in Chrome

谷歌“google chrome onsubmit javascript”

时需要阅读很多内容

编辑添加

<html>
<head>
<script type="text/javascript">
    function validate(){
        if(
        document.getElementById('device_name').value.trim() === "" ||
        document.getElementById('muid').value.trim() === "" ||
        document.getElementById('map_version').value.trim() === "0.000"
        )
        {
            alert("Please fill out all fields before clicking submit!");
            return false;
        }
        else
        {
            alert("Hold on just adding to database :)");
            return true;
        }
    }
</script>


    <title></title>
</head>
<body>
    <a href="index.xhtml">Mogadishu</a><br>
    <form action="vulcanoprocess.php" method="post" onsubmit="return validate()" name="vulcanoForm">
        <fieldset>
            <legend>Device Information</legend> <label for="device_name">Device Name:</label><br>
            <input type="text" name="device_name" id="device_name"><br>
            <label for="muid">MUID:</label><br>
            <input type="text" name="muid" id="muid"><br>
            <input type="submit">
        </fieldset>
    </form>
</body>

答案 1 :(得分:0)

要进行调试,请使用FireBug作为Firefox添加,并选择javascript选项卡以查找错误或“控制台”。使用该信息来修复代码中的错误以及它们何时消失以测试chrome中的脚本。如果仍有错误,可能是因为Chrome浏览器缺少与js的某种兼容性。