我正在尝试创建一个带有几个问题的调查。我需要它来跟踪每个选择有多少票。然后查看所有结果。我不确定我做错了什么。当我单击“查看结果”或“提交”时,它根本不会调用这些函数。为什么?
<body>
<form id="surveyForm">
<ul>
<li>What is your favorite color?</li>
<li><input type="radio" id="red" name="red" value="red"><for name="red">Red</for></li>
<li><input type="radio" id="green" name="green" value="green"><for name="red">Green</for></li>
<li><input type="radio" id="blue" name="blue" value="blue"><for name="blue">Blue</for></li>
</ul>
<input type="button" id="viewSurvey" value="View Survey Results">
<input type="button" id="submitSurvey" value="Submit">
</form>
<script>
var redCount = 0;
var greenCount = 0;
var blueCount = 0;
if (window.addEventListener)
addEvent = function(ob, type, fn ) {
ob.addEventListener(type, fn, false );
};
else if (document.attachEvent)
addEvent = function(ob, type, fn ) {
var eProp = type + fn;
ob['e'+eProp] = fn;
ob[eProp] = function(){ob['e'+eProp]( window.event );};
ob.attachEvent( 'on'+type, ob[eProp]);
};
function $(id) {
return document.getElementById(id);
}
var redColor = $('red');
var greenColor = $('green');
var blueColor = $('blue');
var viewSurvey = $('viewSurvey');
var submitSurvey = $('submitSurvey');
var formSurvey = $('surveyForm');
function showSurvey() {
alert(redColor + " " + greenColor + " " + blueColor);
return false;
}
function sendSurvey() {
if($(redColor).selected) {
redColor += 1;
} else if($(greenColor)) {
greenColor += 1;
} else if($(blueColor)) {
blueColor += 1;
}
return false;
}
addEvent(viewSurvey, 'onclick', showSurvey);
addEvent(submitSurvey, 'onclick', sendSurvey);
</script>
</body>
</html>