I have an img that when clicked, points to a form using a custom attribute, and that form then submits it’s hidden inputs via .post
Here is the code:
$("img.imgsubmit").live('click',function(){
var msg = $(this).attr('title');
var r=confirm(msg+'?');
if (r==false){return false;}
var form = '#'+$(this).attr('imgsubmit');
var url = $(form).attr('action');
var output = OutputName($(form).attr('output'));
$("#"+output).html('<img src="images/ajax-loader.gif" alt="Working..." />');
// get post values
var data = {}; // define data object
$(':input',this).each(function(index){
alert(this); // NEVER HAPPENS
var key = $(this).attr('name');
var val = $(this).val();
data[key] = val;
});
// post
$.post(url,data, function(html) {
$("#"+output).html(html).slideFadeShow("fast");
});
return false;
});
So everything is fine until:
$(':input',form).each(function(index){
alert(this); // NEVER HAPPENS, so it's never finding all the inputs
var key = $(this).attr('name');
var val = $(this).val();
data[key] = val;
});
I’ve tried all sorts of things with that first line:
$(':input',$(form)).each(function(index){
$(':input',form).each(function(index){
$(form+' :input').each(function(index){
None of it gets it to work. Basically, it’s not working with the object at all to select the hidden inputs inside the form element.
tl;dr everything is fine except the :input selector not working when accessing the form var.
Just a stab in the dark without seeing your markup but $(“form :input”) should work.
note that it’s all in quotes