Javascript onClick计数器不起作用

时间:2018-05-06 19:20:11

标签: javascript html

我正在编写Like-Hate的javascript计数器。

我的HTML代码如下。 (该代码是从PHP代码解析的)

SELECT
bill.id as bill_id,
bill.user_id as bill_user_id,
bill.branch_id as bill_branch_id,
bill.time as bill_time,
sales.id as sales_id,
item_id,
quantity,
price,
min(status) AS status,
ANY_VALUE(sales_plus.user_id) AS sales_plus_user_id,
ANY_VALUE(sales_.branch_id) AS sales_plus_branch_id,
ANY_VALUE(sales_.time) AS sales_plus_time,
FROM sales
INNER JOIN sales_bill
ON sales.bill_id = bill.id
LEFT JOIN sales_plus
ON sales.id = sales_plus.sales_id
WHERE bill_id = :bill_id
GROUP BY sales.id

onClick javascript代码也一样。

<img id='like1' src='./imoticon/good.png' style='width:23px;height:23px;' title='좋아요' value='1' onclick='onClick(this.value)'>좋아요
<img id='hate1' src='./imoticon/bad.png' style='width:23px;height:23px;' onclick='onClick(this.value)' value='-1' title='싫어요'>싫어요

但是计数器不起作用。 请帮我一个建议。

3 个答案:

答案 0 :(得分:1)

使用this代替this.value并在javascript中使用getAttribute方法

<img id='like1' src='./imoticon/good.png' style='width:23px;height:23px;' title='좋아요' value='1' onclick='onClick(this)'/>좋아요
<img id='hate1' src='./imoticon/bad.png' style='width:23px;height:23px;' onclick='onClick(this)' value='-1' title='싫어요'>싫어요/>

在javascript中:

function onClick(int) {
//alert('click');
console.log(int.getAttribute('value'));
}

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<img id='like1' src='./imoticon/good.png' style='width:23px;height:23px;' title='좋아요' value='1' onclick='onClick(this)'/>좋아요
<img id='hate1' src='./imoticon/bad.png' style='width:23px;height:23px;' onclick='onClick(this)' value='-1' title='싫어요'>싫어요/>




<script>
function onClick(int) {
alert(int.getAttribute('value'));
console.log(int.getAttribute('value'));
}
</script>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我的HTML代码位于

之下
.....
<select id="LikeHate">
<option src="./imoticon/good.png" style="background-image:url(like.png);width:23px;height:23px;" onclick="onClick1(this.value)" title="좋아요" value="1" />좋아요
<option src="./imoticon/good.png" style="background-image:url(bad.png);width:23px;height:23px;" onclick="onClick2(this.value)" title="싫어요" value="-1" />싫어요
</select>
.....

JS代码与

相同
 ...
     var q = document.getElementById("input1").value+"@@"+document.getElementById("txt1").value+"@@"+getCookie("username")+"@@"+document.getElementById("LikeHate").value;
    xmlhttp.open("GET", "inputComment.php?q=" +q, true);
   ...

在PHP中,在爆炸(“@@”,$ q)之后,最后一个值将是一个整数并总结为数据文件。

答案 2 :(得分:0)

这是我认为最简单和最好的答案。

enter image description here

由于上述选择。

enter image description here

每个人都想要上面的图片结果。

我用手指指着月亮,看到我的手指错了。我们需要一种更具包容性的未来姿态。我很感激每个人的责备。