Variable field names in $.get in jQuery
I'm trying to get my .get call to send the 开发者_如何转开发name of the input field used (In this case a select) as the GET parameter key, and the value of the select as the value of the corresponding GET parameter. However, I can't seem to get variable GET keys to work, as jQuery whines about "missing : after property id".
My code looks somewhat like this:
$('.feedback_select').change(function() {
$.get(
'/feedback',
{
$(this).attr('name') : $(this).val()
}
);
});
var data = {};
data[$(this).attr('name')] = $(this).val();
Then pass data
to $.get()
.
PS: You can simply do this.name
to avoid creating two jQuery objects. Or you could do var $this = $(this);
and then use $this
instead of $(this)
.
You need to build the object by hand:
var params = {};
params[$(this).attr('name')] = $(this).val();
精彩评论