JavaScript没有在onClick上执行

时间:2014-05-10 19:10:35

标签: javascript html forms

我有一个网页,我正在使用JavaScript和onClick处理HTML表单,但JavaScript代码没有被执行。

这是我的表格:

<form action="" method="post" id="reportform">
    <input type="radio" name="report" value="customer"><p>Customers</p>
    <input type="radio" name="report" value="item"><p>Items Sold</p>
    <input type="radio" name="report" value="department"><p>Sales Departments</p>
    <input type="radio" name="report" value="person"><p>Sales People</p>
    <input type="button" name="reportsubmit" value="Submit" onClick="checkReport(this.form)">
</form>

这是JavaScript代码(通过这种方式,JavaScript和表单都位于body标签中):

function checkReport (form) 
{
    var checked = form.querySelector('input:checked');
    var value = checked ? checked.value : null;

    if (value == "customer") 
    {
        var href = "http://bdpastudents.com/~a7068104/2013-2014/Lab_13/Reports/Reports.php";
        var idx = href.indexOf("Reports", 0);
        var new_href = href.slice(0,idx+19) + "#customer" + href.slice(idx+19);

        window.location.replace(new_href);
    } 
    else if (value == "item") 
    {
        var href = "http://bdpastudents.com/~a7068104/2013-2014/Lab_13/Reports/Reports.php";
        var idx = href.indexOf("Reports", 0);
        var new_href = href.slice(0,idx+19) + "#item" + href.slice(idx+19);

        window.location.replace(new_href);
    } 
    else if (value == "department") 
    {
        var href = "http://bdpastudents.com/~a7068104/2013-2014/Lab_13/Reports/Reports.php";
        var idx = href.indexOf("Reports", 0);
        var new_href = href.slice(0,idx+19) + "#department" + href.slice(idx+19);

        window.location.replace(new_href);
    } 
    else if (value == "person") 
    {
        var href = "http://bdpastudents.com/~a7068104/2013-2014/Lab_13/Reports/Reports.php";
        var idx = href.indexOf("Reports", 0);
        var new_href = href.slice(0,idx+19) + "#person" + href.slice(idx+19);

        window.location.replace(new_href);
    }
}

function checkCustomer(form) 
{
    var checked = form.querySelector('input:checked');
    var value = checked ? checked.value : null;

    if (value == "all") 
    {
        var href = "http://bdpastudents.com/~a7068104/2013-2014/Lab_13/Reports/Reports.php";
        var idx = href.indexOf("Reports", 0);
        var new_href = href.slice(0,idx+19) + "#allcustomers" + href.slice(idx+19);

        window.location.replace(new_href);
    } 
    else if (value == "one") 
    {
        var href = "http://bdpastudents.com/~a7068104/2013-2014/Lab_13/Reports/Reports.php";
        var idx = href.indexOf("Reports", 0);
        var new_href = href.slice(0,idx+19) + "#onecustomer" + href.slice(idx+19);

        window.location.replace(new_href);
    }
}   

1 个答案:

答案 0 :(得分:-1)

您的HTML是非法的。您尚未关闭输入标记。 谁知道,你不必关闭你的标签。对不起。

你可以在这个小提琴上复制你的问题吗?或者这对你有用吗?

var form = document.getElementById('reportForm');

http://jsfiddle.net/mgDGN/