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 :)

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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) { ... }
     });
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.