如何使按钮根据JavaScript的布尔值执行两件事

时间:2019-02-07 10:50:34

标签: javascript model-view-controller

我正在尝试在我的网站上创建民意调查功能。如果用户按下按钮时尚未投票,我希望计数增加1;如果按下按钮时用户已经投票,我希望计数减少1。现在,它会上升到无穷远,永远不会达到-1。

如果我将if(voted)更改为if(!voted),则会出现相反的问题,并且每次单击都变为-1。

var voted = new Boolean(false);

        $(document).ready(function () {
            $("#container div button").click(function () {
                if (voted) {
                    $(this).parent().animate({
                        width: '+=100px'
                    }, 500);

                    $(this).prev().html(parseInt($(this).prev().html()) + 1)
                    voted === true;

                } else {
                    $(this).parent().animate({
                        width: '-=100px'
                    }, 500);
                    $(this).prev().html(parseInt($(this).prev().html()) - 1)
                    voted === false;

                }

            });
        });

2 个答案:

答案 0 :(得分:6)

voted === true应该是voted = true

voted === false应该是voted = false

您必须重新分配voted变量,而不是检查是否相等。

看看assignment operatorequality operator在js中如何工作

答案 1 :(得分:0)

rotation_quat = Quaternion(0.999999913938509, 0.00029714546497339216, -0.00011465939948083866, 0.0002658585515330323) rotation = rotation_quat.rotation_matrix.astype(np.float64) translation = np.array([0.2,0.2,0.8]).astype(np.float64) R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(intrinsicMatrix1, distCoeffs1, intrinsicMatrix2, distCoeffs2, img1.shape[::-1], rotation, translation, alpha=1) map11, map12 = cv2.initUndistortRectifyMap(intrinsicMatrix1, distCoeffs1, R1, P1, img1.shape[::-1], cv2.CV_32FC1) map21, map22 = cv2.initUndistortRectifyMap(intrinsicMatrix2, distCoeffs1, R2, P2, img2.shape[::-1], cv2.CV_32FC1) # rectify img1_rect = cv2.remap(img1, map11, map12, cv2.INTER_LANCZOS4) img2_rect = cv2.remap(img2, map21, map22, cv2.INTER_LANCZOS4) cv2.imshow("img1_rect", img1_rect) cv2.waitKey(0) cv2.imshow("img2_rect", img2_rect) cv2.waitKey(0) voted === true是比较。

您应该使用voted === falsevoted = true(这是适当的分配)