代码:
https
://dl.dropboxusercontent.com/u/16952797/webdev/uppg1/kontakt.html
(https://dl.dropboxusercontent.com/u/16952797/webdev/uppg1/kontakt.js)
http:// jsfiddle.net/mptA5/
相关代码部分:
function addEvent(element, eventType, theFunction, capture)
{
if(element.addEventListener)
{
element.addEventListener(eventType, theFunction, capture);
}
else if(element.attachEvent)
{
element.attachEvent( "on" + eventType, theFunction);
}
}
function validateInput(form)
{
switch(form)
{
case "other":
if (document.getElementById("textareaOtherComment").value == "")
{
alert("Vänligen fyll i kommentarfältet!");
return false;
}
if (document.getElementById("inputEmail").value == "")
{
alert("Vänligen fyll i mejlfältet!");
return false;
}
return true; // NOTE: breaks aren't necessary after return statements
case "sugg":
if (document.getElementById("selectSuggestion").selectedIndex == 0)
{
alert("Vänligen välj en förslagskategori!");
return false;
}
if (document.getElementById("textareaSuggestionComment").value == "")
{
alert("Vänligen fyll i kommentarfältet!");
return false;
}
return true;
default:
return false;
}
}
function preventDefaultAction(event)
{
if (event.preventDefault)
{
event.preventDefault();
}
else if (event.returnValue)
{
event.returnValue = false;
}
}
function preventDefaultEvents()
{
addEvent(document.getElementById("btnOtherSubmit"), "click", preventDefaultAction, false);
addEvent(document.getElementById("btnBugSubmit"), "click", preventDefaultAction, false);
}
function addEventListeners()
{
.
.
.
addEvent(document.getElementById("btnOtherSubmit"), "click", function(){submitForm("other");}, false);
.
.
.
addEvent(document.getElementById("btnSuggestionSubmit"), "click", function(){submitForm("sugg");}, false);
.
.
.
}
function init()
{
.
.
.
preventDefaultEvents();
addEventListeners();
}
上下文:我的“建议”(Lämna ett förslag)和“其他”(övrigt)表单在 chrome(W3C 浏览器)中按预期工作,但在 IE(用 IE8 测试)中,“其他”表单在 buttonclick 上提交,即使输入没有通过验证过程......我在IE的F12开发者工具中调试了“建议”表单和“其他”表单,似乎在按钮单击时,“其他”表单进入了preventDefaultAction函数,而“建议”表单不会在验证失败时出现。