jQuery:编辑$ .get返回的数据字符串

时间:2013-10-30 08:41:10

标签: javascript jquery

我想从data修改返回的$.get。我坚持不知道如何应用新创建的inputname,我认为这个问题与$(data)有关。

id = '12';
$.get('/page.php?id='+id, function(data) {

        // split input name by hyphen
        m = $(data).find('input').attr('name').split('-');

        // build new input name attribute 
        inputname = m[0] + '-' + id + '-' + m[2] + '-' + m[3] + '-' + m[4] + '-' + m[5];

        // apply new input name to data ??? this part I'm stuck on.
        $(data).find('input').attr('name', inputname);

});

1 个答案:

答案 0 :(得分:1)

尝试

id = '12';
$.get('/page.php?id=' + id, function (data) {
    var $data = $(data);

    // split input name by hyphen
    $data.find('input').attr('name', function (idx, name) {
        var m = name.split('-');
        var inputname = m[0] + '-' + id + '-' + m[2] + '-' + m[3] + '-' + m[4] + '-' + m[5];
        return inputname;
    });

    //be careful that the contents of `data` still will not have the updated name, but the contents of the jQuery wrapper $data will have the updated name
});