Javascript onClick功能无法正常工作

时间:2015-10-21 06:15:17

标签: javascript html

    <!DOCTYPE html>
    <html>
        <head>
            <title> Delete Paper </title>
        </head>
        <body>
            <script>
                    document.write('<?php 
                        session_start();
                        $username = "";
                        $password = "";
                        $server = "localhost";
                        $user = "";
                        $pass = "";
                        $db = "test";
                        $userid = $_SESSION['userid'];


                        $conn = new mysqli($server,$user,$pass, $db);
                        $sql = "SELECT * FROM questionpapercount where userid='$userid'";
                        $result = $conn->query($sql);

                        while($row = $result->fetch_assoc()){
                            $paper = $row['paperid'];
                            echo "<input type=checkbox name=$paper /> $paper ";
                        }

                        $conn->close();

                     ?> ');
                    function deletequery(){
                        alert('abcd');
                    }
                </script>
            <input type=button value=Delete onclick=deletequery() />
        </body>
    </html>

我的javascript功能无法正常工作。如果我写一个简单的警报(&#39; abcd&#39;);然后它工作正常,但如果我写一个函数而不是它不起作用。

2 个答案:

答案 0 :(得分:0)

JavaScript中的

delete is a operator,所以这些行无法工作:

function delete(){
    alert('abcd');
}

document.write("<input type=button value=Delete onclick=delete(); />");

重命名您的函数以修复您的代码。您还应该将HTML属性放在引号中:

function deleteHandler(){
    alert('abcd');
}

document.write('<input type="button" value="Delete" onclick="deleteHandler();" />');

答案 1 :(得分:-1)

删除是一个关键字,因此您可以使用&#34; del&#34;或其他名称而不是删除。