按钮在春天不会w

时间:2017-07-14 03:28:25

标签: javascript jquery spring jsp button

在jsp文件中,在javascript中

<c:set var="like" value="${like}"/>
<c:set var="unlike" value="${unlike}"/>
<script src="resources/js/jquery-1.9.0.js" type="text/javascript"></script>
<script type="text/javascript">
var like=${like};
var unlike=${unlike};
var no=${vdto.no}
/* like & unlike */
$(function(){
if(like==0||like==null){
$("#like").hide();
$("#like_disabled").show();
}   
else{   
$("#like_disabled").hide();
$("#like").show();
}

if(unlike==0||like==null){
$("#unlike").hide();}
else{   
$("#unlike_disabled").hide();
}   
});
function like()
{like--;location.href="tftube_videoView?no="+no+"&like="+like;}
function like_disabled()
{like++;location.href="tftube_videoView?no="+no+"&like="+like;}

function unlike(){unlike--;location.href="tftube_videoView?                
no="+no+"&unlike="+unlike;}
function unlike_disabled(){unlike++;location.href="tftube_videoView?
no="+no+"&unlike="+unlike;}

在jsp文件中,在jsp代码中

<button id="like_disabled" onclick="like_disabled()">like</button>
<button id="like" onclick="like()" >like</button><!-- it's not work -->
<button id="unlike_disabled" onclick="unlike_disabled()"         
value="unlike">unlike</button>
<button id="unlike" onclick="unlike()" >unlike</button>

在控制器中

//request
@RequestMapping(value="/tftube_videoView", method=RequestMethod.GET)
public ModelAndView tftube_videoView(HttpServletRequest arg0, 
                            HttpServletResponse arg1) throws Exception {
    ModelAndView mv=new ModelAndView();
    HttpSession session=arg0.getSession();

    String no_raw=arg0.getParameter("no");//number of video
    int no=0;
    if(no_raw!=null){
    no=Integer.parseInt(no_raw);}//tftube_video

    VideoDTO vdto=videoDAO.getVideo(no);//a video's total information

    String video_name=vdto.getVideo_name();

    String like_stat_raw=arg0.getParameter("like");
    String unlike_stat_raw=arg0.getParameter("unlike");


    System.out.println("request like:"+like_stat_raw);
    System.out.println("request unlike:"+unlike_stat_raw);

    int res_like=0;
    int res_unlike=0;
    /*VideoDTO likedto=new VideoDTO();*/
    if(like_stat_raw!=null){
        int like_stat=Integer.parseInt(like_stat_raw);
        System.out.println("들어온 like 숫자 변환"+like_stat);
            switch(like_stat){
            /*case 1:res_like=videoDAO.click_like(no);break;*/
            case 0:res_like=videoDAO.cancel_like(no);break;
            default:res_like=videoDAO.click_like(no);break;
            }
    }

    if(unlike_stat_raw!=null){
        int unlike_stat=Integer.parseInt(unlike_stat_raw);          
        switch(unlike_stat){
        /*case 1:res_unlike=videoDAO.click_unlike(1);break;*/
        case 0:res_unlike=videoDAO.cancel_unlike(0);break;
        default:res_like=videoDAO.click_like(no);break;
        }           
    }
    //response
            int like_status=videoDAO.getVideo(no).getLike_status();
            int unlike_status=videoDAO.getVideo(no).getUnlike_status();
            System.out.println("response like:"+like_status);
            System.out.println("response unlike:"+unlike_status);
            mv.addObject("like",like_status);
            mv.addObject("unlike",unlike_status);       
            //end of response   

其余部分被省略。

首先,结果中有两个方框(按钮&#39;喜欢&#39;和&#39;不像&#39;隐藏)
如果我点击按钮&#39; id = like_disabled&#39;,它的事件将会运行,它将隐藏并按下“id = like&#39;会显示。
到目前为止,这不是问题,但如果我点击按钮&#39; id = like&#39;,它就无法正常工作。
为什么??请教我解决方案。

1 个答案:

答案 0 :(得分:0)

问题是您使用相同的名称 - like - 用于变量和方法。这完全是一种不好的做法。

您首先将like声明为变量,然后再将其声明为方法。当like_disabled被调用时,您正在执行like--,从那一刻开始,like不再是一种方法。我认为你应该在控制台上获得TypeError

所以,停止对变量和方法使用相同的名称。