Solved

jquery on submit not running

Posted on 2014-02-06
4
338 Views
Last Modified: 2014-02-14
I can't figure out what I am missing.  The page is called index.html.  
When i click the submit button, the jquery submit code does not seem to be running at all.  The text boxes don't get the value they should and the alerts are not even firing.

eventually when this is working the action is going to be changed to a php file that will use the numbers in the text boxes.

What am I missing???

<html>
<head>
	<style type="text/css">
		.option { 
			text-align: center;
		}

		th {
			width: 100px;
			background-color:#6699FF;
		}

		th.sectionHeader {
			text-align: left;
			font-size: 20;
			background-color:transparent;
		}

		.altShade {
			background-color:#ABC6BD;
		}
	</style>

	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	<script>
	$('#survey').submit(function()
	{ alert("got here");
		var result = 0;
		for (i=1; i<8; i++)
		{
			result = 0;
			$(".S"+i+":checked").each(function () {
				result += parseFloat($(this).val());
			});
			alert(result);
			$("#sectionTotal"+i).val(result);
		}
		alert(result);
		return true; 
	});
	</script>

</head>

<body>		
	<form id="survey" action="index.html" method="post">

	<table id='section1' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 1 Question 1</td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 1 Question 2</td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="1"/></td>
		</tr>
		<tr>
			<td class='question'Section 1 Question 3</td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal1' value=''/>
	</table>

	<table id='section2' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 1</td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 2 Question 2</td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="1"/></td>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 3</td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal2' value=''/>
	</table>

	</br></br>
	<input type="submit" value="View your results">
	</form>
</body>
</html>

Open in new window

0
Comment
Question by:KCTechNet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Assisted Solution

by:Melih SARICA
Melih SARICA earned 250 total points
ID: 39841170
When u set the submit of the form. Form on Js side is createted yet.

Move ur <script> block after the form.. then u ll see its working

<html>
<head>
	<style type="text/css">
		.option { 
			text-align: center;
		}

		th {
			width: 100px;
			background-color:#6699FF;
		}

		th.sectionHeader {
			text-align: left;
			font-size: 20;
			background-color:transparent;
		}

		.altShade {
			background-color:#ABC6BD;
		}
	</style>

	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	
</head>

<body>		
	<form id="survey" action="index.html" method="post">

	<table id='section1' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 1 Question 1</td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q1" class="S1" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 1 Question 2</td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q2" class="S1" value="1"/></td>
		</tr>
		<tr>
			<td class='question'Section 1 Question 3</td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="5"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="4"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="3"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="2"/></td>
			<td class='option'><input type="radio" name="S1Q3" class="S1" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal1' value=''/>
	</table>

	<table id='section2' border='0'>
		<tr>
			<th class='sectionHeader'>Teamwork</th>
			<th>Strongly Disagree</th>
			<th>Disagree</th>
			<th>Neither</th>
			<th>Agree</th>
			<th>Strongly Agree</th>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 1</td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q1" class="S2" value="1"/></td>
		</tr>
		<tr class='altShade'>
			<td class='question'>Section 2 Question 2</td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q2" class="S2" value="1"/></td>
		</tr>
		<tr>
			<td class='question'>Section 2 Question 3</td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="5"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="4"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="3"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="2"/></td>
			<td class='option'><input type="radio" name="S2Q3" class="S2" value="1"/></td>
		</tr>
		<input type='text' id='sectionTotal2' value=''/>
	</table>

	</br></br>
	<input type="submit" value="View your results">
	</form>
<script>
	$('#survey').submit(function()
	{ alert("got here");
		var result = 0;
		for (i=1; i<8; i++)
		{
			result = 0;
			$(".S"+i+":checked").each(function () {
				result += parseFloat($(this).val());
			});
			alert(result);
			$("#sectionTotal"+i).val(result);
		}
		alert(result);
		return true; 
	});
	</script>

</body>
</html>

Open in new window

0
 

Author Comment

by:KCTechNet
ID: 39841240
that did the trick.  

But I notice that $_POST["sectionTotal1"] has no value on the 'receiving' php page, but  $_POST["S1Q1"] does.  I am guessing that is because the sectionTotal1 is populated during the submit and not prior to??  Although when I change the submit function to return false I see that the text boxes are populated correctly.
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 39841251
Two things

1. Although having your scripts at the end of your page is a good practice, you should also wrap your jquery script in document ready code like shown below. This will guarantee that your code does not run until the page has loaded.
$(function() {
  $('#survey').submit(function()  {
  alert("got here");
    var result = 0;
    for (i=1; i<8; i++)
    {
      result = 0;
      $(".S"+i+":checked").each(function () {
        result += parseFloat($(this).val());
      });
      alert(result);
      $("#sectionTotal"+i).val(result);
    }
    alert(result);
    return true; 
  });
});

Open in new window


2. Your sectionTotal value is not coming through because the element does not have a name.  Change to
<input type='text' id='sectionTotal1' name='sectionTotal' value=''/>

Open in new window


That should resolve both issues
0
 

Author Comment

by:KCTechNet
ID: 39841878
Ahh.   I missed that!!  Thanks
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question