简体   繁体   English

为什么此代码不起作用(js函数)?

[英]Why isn't this code working (js function)?

I'm working on Java EE web app and need to working with js also.There is two buttons on app jsp page :one of them upVote and downVote.I want to make the button call '${upVote}' at first click and call '${downVote}' when the same user clicking for second time. 我正在使用Java EE Web应用程序,还需要使用js。应用程序jsp页面上有两个按钮:其中一个是upVote和downVote。我要在第一次单击时使按钮调用'$ {upVote}'当同一用户第二次单击时,调用“ $ {downVote}”。 The logic of the process is to ensure that each user can vote one time per question.But I don't know why is not working as desired. 该过程的逻辑是确保每个用户可以对每个问题投票一次。但是我不知道为什么不能按要求进行。

So I need to help about this.All answers appreciated. 因此,我需要对此提供帮助。感谢所有答复。

My Code : 我的代码:

    <c:url var="User" value="/AQ/User">
        <c:param name="answerUserId" value="${question.USERID}" />
    </c:url>
    <c:url var="Question" value="/AQ/UpdateQuestion">
        <c:param name="questionId" value="${question.ID}" />
    </c:url>
    <c:url var="upVote" value="/AQ/UpVoteQuestion">
        <c:param name="questionId" value="${question.ID}" />
        <c:param name="qUserId" value="${question.USERID}"/>
    </c:url>
    <c:url var="downVote" value="/AQ/DownVoteQuestion">
        <c:param name="questionId" value="${question.ID}" />
        <c:param name="qUserId" value="${question.USERID}"/>
    </c:url>

<script type="text/javascript">
    var voteCounter = 0;
    var votedQuestion = 0; 

    function control() {     

        if('${sessionScope[userId]}' <= 0){window.location.href='SignIn'}
        else if(voteCounter >= 1 && votedQuestion == '${question.ID}') {window.location='${downVote}';}
        else{
            voteCounter++;
            votedQuestion = '${question.ID}';
            window.location='${upVote}';
             return false;
        }
    }

    function reverse() {
        if('${sessionScope[userId]}' <= 0){window.location.href='SignIn'}
        else if(voteCounter >= 1 && votedQuestion == '${question.ID}') {window.location='${upVote}';}
        else{
            voteCounter++;
            votedQuestion = '${question.ID}';
            window.location='${downVote}';
             return false;
        }
    }
</script>

And there is my buttons : 还有我的按钮:

<pre style="max-width: 35px; margin-right: 10px;">

                <input class="vote-img" type="image"
                    src="${pageContext.request.contextPath}/resources/images/upVote.png"
                    id="vote" onclick="control()"/>
                <br>
                <label style="float: left; margin-left: 18px;"><c:out value="${question.VOTE }" /></label>
                <input class="vote-img" type="image"
                    src="${pageContext.request.contextPath}/resources/images/downVote.png"
                    id="vote" onclick="reverse()"/>
</pre>

window.location正在刷新您的浏览器,这意味着js将再次加载,并且每次表决数== 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM