0

我是网页设计/开发的新手;我有以下代码来检查是否在文本框中输入了数据。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>
4

2 回答 2

0

Onsubmit 在 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>

于 2012-10-22T14:25:51.063 回答
0

To debug, use FireBug as a Firefox add on and select the javascript tab for errors or 'console'. Use that information to fix errors in code and when they go away test the script in chrome. If there are still errors, it might be because the chrome browser is missing some type of compatibility with js.

于 2012-10-22T18:22:47.120 回答