开发者

jquery pass dynamic variable name

开发者 https://www.devze.com 2023-01-02 07:25 出处:网络
function liveUpdate(fld,value,id) { $.ajax({ type: \'POST\', url: \'myurl.html\', data: { fld:value, \'id\': id },

function liveUpdate(fld,value,id) {

    $.ajax({
      type: 'POST',
      url: 'myurl.html',
      data: { fld:value, 'id': id },
      success: function(data){//console.log(data);
      }
    });

    }

i want fld to be posted as fld's value not the variable name fld? i've tried to wrap around with eval but no luck

开发者_如何学JAVA

any ideas?

thanks


You could do something like this:

function liveUpdate(fld, value, id) {
    var data={id: id};
    data[fld]=value;
    $.ajax({
        type: "POST",
        url: "myurl.html",
        data: data,
        success: function(data) {
            //console.log(data);
        }
    });
}


you need to modify the following line.

data: { fld:fld, id: id },


var data = { id : id };
data[fld] = value;

$.ajax({ ..., data : data });
0

精彩评论

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