admin管理员组文章数量:1310519
The code I'm using is supposed to change the 'action' attribute for a form based on radio button selection. It doesn't work at the moment and I'm trying to work out why. According to my Firefox error console, the function 'submitForm' is not defined - but I'm sure it is defined!!
Can anyone help?
Code as follows:
Inside the head (q1 refers to the name attribute of the radio buttons):
<script type="text/javascript" src="scripts/javascript.js">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
And the event handler:
<form method="post" id="question1" onsubmit="return submitForm();">
The code I'm using is supposed to change the 'action' attribute for a form based on radio button selection. It doesn't work at the moment and I'm trying to work out why. According to my Firefox error console, the function 'submitForm' is not defined - but I'm sure it is defined!!
Can anyone help?
Code as follows:
Inside the head (q1 refers to the name attribute of the radio buttons):
<script type="text/javascript" src="scripts/javascript.js">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
And the event handler:
<form method="post" id="question1" onsubmit="return submitForm();">
Share
Improve this question
asked Jan 20, 2012 at 15:43
ChrisChris
1,8836 gold badges30 silver badges44 bronze badges
1
- Does this answer your question? JavaScript: Inline Script with SRC Attribute? – Ivar Commented May 23, 2023 at 11:04
3 Answers
Reset to default 6You can't have a cript tag with a src
and script contents. Your browser is loading the script file, and ignoring the contents inside, thus your function is undefined. this is what you want:
<script type="text/javascript" src="scripts/javascript.js"></script>
<script type="text/javascript">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
Perhaps this is what you want:
<script type="text/javascript" src="scripts/javascript.js"></script>
<script type="text/javascript">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
In general, you cannot have a script tag with an src and contents within the tag. Most browsers will see the src and ignore what's within the tag itself, or throw an error. See What does a script-Tag with src AND content mean?
Are you actually writing the code inside that script tag, or is that ripped from firefox?
Because you can't write JavaScript inside a script tag with a src
attribute. This is how it should look:
<script type="text/javascript" src="scripts/javascript.js"></script>
<script type="text/javascript">
function submitForm() {
if (document.forms[0].q1[1].checked == true) {
document.forms[0].action = "q2.html";
}
else if (document.forms[0].q1[0].checked == true) {
document.forms[0].action = "q3.html";
}
else {
alert ('Please choose an option');
}
}
</script>
本文标签:
版权声明:本文标题:javascript - Why am I seeing 'function not defined' on my error console when I'm sure my function IS def 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741806265a2398521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论