Reputation: 91
$(".form").submit(function(e){
e.preventDefault();
$.ajax({
url: "file.php",
type: "POST",
data: {
input_value: $(".form").find("input:not(:first-child)").filter(function(){
return this.value.length !== 0;
}).val(),
input_name: $(".form").find("input:not(:first-child)").filter(function(){
return this.value.length !== 0;
}).name,
},
success: function(data) {
},
});
});
okay, so I'm submitting a form with several inputs, but only one with a value that isn't blank.
So, inside data, my 'input_value' variable is working fine, it finds the only input with a value and stores that value.
I try the same idea to get the input's 'name' attribute (inside input_name variable), but my php file gives me a 'Undefined index' error...
I don't understand why it works for the input's value, and not for its name...
oh, by the way, the inputs are generate dynamically (not sure if it has something to do with that...)
Thanks so much!!
Upvotes: 0
Views: 79
Reputation: 318182
There is no .name property directly on a jQuery object, you have to use prop()
$(".form").find("input:not(:first-child)").filter(function(){
return this.value.length;
}).prop('name'),
Upvotes: 2