Solved

jquery on submit not running

Posted on 2014-02-06
4
328 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
  • 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 51

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now