Jump to content

Can i use a single jquery method for all fields in my form?


lonewolf217

Recommended Posts

I have a form with all types of inputs .. text, select, checkbox, radio

 

Is there a particular method that I could use that will trigger if any of them are changed, or will I have to call different methods for each type of input (i.e. keyup for text fields, click for submit button)

 

say for example, can I assign a class to every input and then trigger a type of generic onchange event for that class ?

 

Go easy on me, I am working through about a dozen tutorials online right now that each handle jquery a different way and most of them seem to only do a simple thing like generate an email based on text fields.  hopefully google will come through for me soon, but its always nice to have a fallback :)

well right now I am triggering off clicking the submit button.  It kicks off a POST request to the backend page which runs the SQL query from the form fields and prints the results in a new div.

 

This is my oversimplified version with only a single text field to test if it works or not, which it does.  Now that I am sure that it works, I am looking to see what I can do with this part to allow it to trigger off of any input type, such as my dropdown menu with ID=test

$("#name").keyup(function() {

 

here is the whole pageif it matters at all

	<script type="text/javascript">
		$(document).ready(function() {
			$("#name").keyup(function() {
				var datastring = $("input#name").val();
				//alert(datastring);return false;
				$.post("test.php",{name:""+datastring+""},function(data) {
					if(data.length > 0) {
						$('#resources').fadeIn('slow');
						$('#resources').html(data);
					}
				});
				return false;	
			});
		});	

	</script>

<form name="form">
    <input type="text" name="name" id="name">
    <select name="test" id="test">
        <option value="1">1</option>
        <option value="1">2</option>
    </select>
    <input type="submit" value="Submit" id="submit">
</form>

You want to fire off a POST request on each keyup? Well, that should work too.

 

Sample code -

jQuery('.form_inputs').keyup(function() {
     $.post("test.php", {name: $(this).val()}, function (data) {
          if (data.length > 0) { ... }
     });
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.