0

我的表单中有两个按钮用于调用两个 JavaScript 函数。第一个按钮在成功onclick调用该函数的事件中运行良好,payroll()但第二个按钮是提交类型,它从不send()在表单提交时调用该函数。我不知道为什么会出现这个问题。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html >
<head>
    <title>hr page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript"
    src="/static/js/sijax/sijax.js"></script>
    <script type="text/javascript">
    {{ g.sijax.get_js()|safe }}</script>
    <link rel="stylesheet" href="{{url_for('static', filename='styles/signupcss.css')}}">

    <script type="text/javascript" >
function payroll() {
        var basic=document.forms["salary"]["bsalary"].value;
        var empid=document.forms["salary"]["empid"].value;
       var ta,hra,da,pf,netsalary,grosssalary;
         if (empid == ""||basic == "") {
        alert("Employee ID and Salary details must be filled out");
        return false;
        }
        if(isNaN(basic))
        {alert("Salary must be in Numbers");
        return false;
        }
       hra=basic*40/100;
        da=basic*15/100;
        pf=basic*12/100;
        basic=parseInt(basic);
        hra=parseInt(hra);
        da=parseInt(da);
        grosssalary=basic + hra + da;
        ta=basic*6.2/100;
        netsalary=grosssalary-ta;



        document.getElementById("hra").innerHTML=hra;
        document.getElementById("ta").innerHTML=ta;
        document.getElementById("da").innerHTML=da;
        document.getElementById("netsalary").innerHTML=netsalary;
        document.getElementById("pf").innerHTML=pf;


        document.getElementById("grosssalary").innerHTML=grosssalary;
         window.alert("HI"+grosssalary);
        return true;


    }
     function send()
    {
        var id = document.forms['salary']['empid'].value;
        var basic =  document.forms['salary']['bsalary'].value;
        var hra =  document.forms['salary']['hra'].value;
        var da =  document.forms['salary']['da'].value;
        var ta =  document.forms['salary']['ta'].value;
        var pf =  document.forms['salary']['pf'].value;
        var gross_sal =  document.forms['salary']['grosssalary'].value;
        window.alert("HI"+gross_sal);
        var net_sal =  document.forms['salary']['netsalary'].value;
        Sijax.request('send',[id, basic, hra, ta, da, pf, gross_sal, net_sal]);

    }

</script>
</head>
<body style="font-family:Lato">
    <div style="padding-left:5%;padding-top:0.2%;height:1%;width:100%;background-color:#11557c">
    <h2>Welcome to HR Department</h2><br>
    </div>
<div style="margin-left:15%" >
    <h2>Name</h2>
<form id="salary" name="salary" style="margin-top: 2%" method="post" onsubmit="return send()" >
    <label id = "empid">Employee ID</label><br>
    <input type = "text" name = "empid" placeholder = "Employee ID" /><br><br>
    <label id = "bsalary">Basic Salary</label><br>
    <input type = "text" name = "bsalary" placeholder = "Basic salary" /><br><br>
    <input type="button" value="Calculate" onclick="return payroll()"><br><br>

    <label for ="hra">House Rent Allowance(HRA)</label>
    <p id="hra" name="hra"></p><br>
    <label for ="ta">Travel Allowance(TA)</label>
    <p id="ta" name="ta"></p><br>
    <label for ="da"> Dearness Allowance(DA)</label>
    <p id="da" name="da"></p><br>
    <label for ="netsalary">Net Salary</label>
    <p id="netsalary" name="netsalary"></p><br>
    <label for ="pf">Provident Fund(PF)</label>
    <p id="pf" name ="pf"></p><br>
    <label for ="grosssalary">Gross Salary</label>
    <p id="grosssalary" name="grosssalary"></p><br><br>
    <input type="submit"  value="Upload Salary">
</form>
    </div>




</body>
</html>
4

4 回答 4

0

好的,快速修复,因为您使用 python 烧瓶库 Sijax 用于 ajax 和 jQuery,您可以send像这样更改您的 javascript 函数:

function send(e){
        e.preventDefault(); //it is as good as returning 
                            //false from the function in all cases

        var id = document.forms['salary']['empid'].value;
        ...
 }

并像这样更改您的onsubmit处理程序声明:

<form id="salary" name="salary" style="margin-top: 2%" method="post" 
          onsubmit="return send(event)" >

请注意,当您停止事件链传播时,您将不得不手动提交表单。因此,您可以.preventDefault根据您的自定义条件修改您的发送功能,否则,让表单提交

于 2016-12-31T07:39:02.543 回答
0

您的代码实际上有效,如果您在堆栈溢出中将此代码作为片段运行,则默认情况下实际上阻止表单提交。尝试在codepen. 我试过了,它确实有效。 http://codepen.io/jhonix22/pen/VPZagb

于 2016-12-31T07:41:13.683 回答
0

你不能<p>像表单元素那样使用元素。您可以创建相应的<input type="hidden">元素并将它们填充到payroll().innerHtml 中,或者在段落中获取值。

PS你实际上有一个TypeError异常,调用未声明的表单元素document.forms['salary']['grosssalary']之类的。

于 2016-12-31T07:51:12.827 回答
0

看一下这个。它远非完美的解决方案,但我认为它有帮助。您不能像访问表单输入元素一样访问这些段落。我不完全确定 Sijax 是什么东西。我相信这只是带有某种 CSRF 安全过滤器的普通 AJAX HTTP 事物。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
    <title>hr page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript"
            src="/static/js/sijax/sijax.js"></script>
    <script type="text/javascript">
        {
            {
                g.sijax.get_js() | safe
            }
        }</script>
    <link rel="stylesheet" href="{{url_for('static', filename='styles/signupcss.css')}}">

    <script type="text/javascript">
        function payroll() {
            var basic = document.forms["salary"]["bsalary"].value;
            var empid = document.forms["salary"]["empid"].value;
            var ta, hra, da, pf, netsalary, grosssalary;
            if (empid == "" || basic == "") {
                alert("Employee ID and Salary details must be filled out");
                return false;
            }
            if (isNaN(basic)) {
                alert("Salary must be in Numbers");
                return false;
            }
            hra = basic * 40 / 100;
            da = basic * 15 / 100;
            pf = basic * 12 / 100;
            basic = parseInt(basic);
            hra = parseInt(hra);
            da = parseInt(da);
            grosssalary = basic + hra + da;
            ta = basic * 6.2 / 100;
            netsalary = grosssalary - ta;

            document.getElementById("hra").innerHTML = hra;
            document.getElementById("ta").innerHTML = ta;
            document.getElementById("da").innerHTML = da;
            document.getElementById("netsalary").innerHTML = netsalary;
            document.getElementById("pf").innerHTML = pf;

            document.getElementById("grosssalary").innerHTML = grosssalary;
            window.alert("HI" + grosssalary);
            return true;
        }

        function send() {
            var id = document.forms['salary']['empid'].value;
            var basic = document.forms['salary']['bsalary'].value;

            var hra = document.getElementById('hra').innerHTML;
            var da = document.getElementById('da').innerHTML;
            var ta = document.getElementById('ta').innerHTML;
            var pf = document.getElementById('pf').innerHTML;
            var gross_sal = document.getElementById('grosssalary').innerHTML;
            window.alert("HI" + gross_sal);
            var net_sal = document.getElementById('netsalary').innerHTML;

            // I think you are missing something here.
            Sijax.request('send', [id, basic, hra, ta, da, pf, gross_sal, net_sal]);
        }
    </script>
</head>
<body style="font-family:Lato">
<div style="padding-left:5%;padding-top:0.2%;height:1%;width:100%;background-color:#11557c">
    <h2>Welcome to HR Department</h2><br>
</div>
<div style="margin-left:15%">
    <h2>Name</h2>
    <form id="salary" name="salary" style="margin-top: 2%" method="post" onsubmit="return false">
        <label id="empid">Employee ID</label><br>
        <input type="text" name="empid" placeholder="Employee ID"/><br><br>
        <label id="bsalary">Basic Salary</label><br>
        <input type="text" name="bsalary" placeholder="Basic salary"/><br><br>
        <input type="button" value="Calculate" onclick="return payroll()"><br><br>

        <label for="hra">House Rent Allowance(HRA)</label><br>
        <p id="hra" readonly name="hra"></p>
        <label for="ta">Travel Allowance(TA)</label><br>
        <p id="ta" readonly name="ta"></p>
        <label for="da"> Dearness Allowance(DA)</label><br>
        <p id="da" readonly name="da"></p>
        <label for="netsalary">Net Salary</label><br>
        <p id="netsalary" readonly name="netsalary"></p>
        <label for="pf">Provident Fund(PF)</label><br>
        <p id="pf" readonly name="pf"></p>
        <label for="grosssalary">Gross Salary</label><br>
        <p id="grosssalary" readonly name="grosssalary"></p><br>
        <input type="button" onclick="send()" value="Upload Salary">
    </form>
</div>


</body>
</html>
于 2016-12-31T08:16:10.490 回答