最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why am I seeing 'function not defined' on my error console when I'm sure my function IS def

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 6

You 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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论