开发者

Variable field names in $.get in jQuery

开发者 https://www.devze.com 2023-02-16 06:25 出处:网络
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 correspondin

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消