0

我想编辑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);

});
4

1 回答 1

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
});
于 2013-10-30T08:44:17.663 回答