使用javascript检测相应的按钮

时间:2014-12-08 14:40:01

标签: javascript jquery forms jsp submit

我有一个带有两个按钮的表单 -

<form:form id="reviewApprvDisapprvForm" modelAttribute="updateProofingForm" method="post">
    <input id="approveButton" onclick="submitForm()" type="image" src="/images/buttons/samplesApprovedButton.png" />
    <br />
    <input id="disapproveButton" onclick="submitForm()" type="image" src="/images/buttons/samplesNotApprovedButton.png" />
</form>

此处一个按钮用于批准,另一个按钮用于拒登。我有Javascript函数"submitForm()",这个按钮名为"onclick"。功能就像这样

function submitForm(){
            //if('approvedButton' is clicked){
               $("#reviewApprvDisapprvForm").attr("action","/secure/userMgmt/roleBasedProofing/updateProofingConfirmMVC.do");
            //}

            $("#reviewApprvDisapprvForm").submit();
        }

在这个函数中,我用javascript设置了动作。在这里,我试图找到哪个按钮点击&#34; submitForm()&#34;方法被调用。有两个按钮 - &#34; approveButton&#34;并且&#34;不赞成按钮&#34;。我怎么能这样做,任何人都可以帮助我吗?

提前致谢

2 个答案:

答案 0 :(得分:1)

我会这样做: 从HTML中删除onclick="",在input元素中设置图像,直接将操作添加到表单中:

<form:form id="reviewApprvDisapprvForm" action="/secure/userMgmt/roleBasedProofing/updateProofingConfirmMVC.do" modelAttribute="updateProofingForm" method="post">
    <input id="approveButton" type="submit"><img src="/images/buttons/samplesApprovedButton.png"/></input>
    <br />
    <input id="disapproveButton" type="image" src="/images/buttons/samplesNotApprovedButton.png" />
</form>

$(document).ready(function() {
    $('#disapproveButton').click(function(){
       //your disapprove logic here

    });
});

答案 1 :(得分:0)

这可以帮助

<form:form id="reviewApprvDisapprvForm" modelAttribute="updateProofingForm" method="post">
    <input id="approveButton" onclick="submitForm('approve')" type="image" src="/images/buttons/samplesApprovedButton.png" />
    <br />
    <input id="disapproveButton" onclick="submitForm('notApprove')" type="image" src="/images/buttons/samplesNotApprovedButton.png" />
</form>

然后

function submitForm(buttonVal){
            if(buttonVal=='approve'){
               $("#reviewApprvDisapprvForm").attr("action","/secure/userMgmt/roleBasedProofing/updateProofingConfirmMVC.do");
            }
           $("#reviewApprvDisapprvForm").submit();

        }

由于