大家帮我看下这段按钮点击事件的JS代码有什么问题吗?为什么点击按钮之后没有反应呢?
<scripttype="text/javascript">document.getElementById("ok").onclick=function(){varvot...
<script type="text/javascript">
document.getElementById("ok").onclick =function() {
var voteNum=0;
var sumScore=0;
var averageScore=0;
var name=document.getElementById("optionTeachers").value;
var score=document.getElementById("inputText").value;
document.cookie=name+"="+score;
//获取cookie字符串
var strcookie = document.cookie;
//将多cookie切割为多个名/值对
var arrcookie = strcookie.split("; ");
//遍历cookie数组,处理每个cookie对
for (var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("=");
//找到名称为userId的cookie,并返回它的值
if (name == arr[0]) {
voteNum++;
sumScore += parseDouble(arr[1]);
}
}
if(voteNum!=0){
averageScore=sumScore/voteNum;
}
document.getElementById("getResult").innerHTML=name+"的投票次数为"+voteNum+",总分为"+sumScore+",平均分为"+averageScore;
}
</script> 展开
document.getElementById("ok").onclick =function() {
var voteNum=0;
var sumScore=0;
var averageScore=0;
var name=document.getElementById("optionTeachers").value;
var score=document.getElementById("inputText").value;
document.cookie=name+"="+score;
//获取cookie字符串
var strcookie = document.cookie;
//将多cookie切割为多个名/值对
var arrcookie = strcookie.split("; ");
//遍历cookie数组,处理每个cookie对
for (var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("=");
//找到名称为userId的cookie,并返回它的值
if (name == arr[0]) {
voteNum++;
sumScore += parseDouble(arr[1]);
}
}
if(voteNum!=0){
averageScore=sumScore/voteNum;
}
document.getElementById("getResult").innerHTML=name+"的投票次数为"+voteNum+",总分为"+sumScore+",平均分为"+averageScore;
}
</script> 展开
2个回答
展开全部
<!DOCTYPE HTML>
<html>
<head>
<title>yugi</title>
<meta charset=UTF-8 />
<style type="text/css">
table {
border: 1px solid black;
border-collapse: collapse;
width: 200px;
}
td {
border: 1px solid black;
}
div.wrap {
margin: auto;
border: 1px solid black;
width: 200px;
}
</style>
<script type="text/javascript">
// writeCookie("myCookie", "my name", 24);
function writeCookie (name, value, hours)
{
var expire = "";
if (hours != null)
{
expire = new Date ((new Date ()).getTime () + hours * 3600000);
expire = "; expires=" + expire.toGMTString ();
}
document.cookie = name + "=" + escape (value) + expire;
};
// alert( readCookie("myCookie") );
function readCookie (name)
{
var cookieValue = "";
var search = name + "=";
if (document.cookie.length > 0)
{
offset = document.cookie.indexOf (search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf (";", offset);
if (end == -1)
end = document.cookie.length;
cookieValue = unescape (document.cookie.substring (offset, end))
}
}
return cookieValue;
};
onload = function ()
{
ok.onclick = function ()
{
var name = optionTeachers.value;
var score = inputText.value;
writeCookie (name, score, 24);
getResult.innerHTML = readCookie (name);
};
};
</script>
</head>
<body>
<div class="wrap">
<table>
<tr>
<td><input type="text" id="optionTeachers" />
</td>
</tr>
<tr>
<td><input type="text" id="inputText" />
</td>
</tr>
<tr>
<td style="float: right;"><input type="button" id="ok" value="ok" />
</td>
</tr>
</table>
<div id="getResult"></div>
</div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询