开发者

Any idea why a variable used in a FOR loop cannot be used in Array? JS + Jquery

I have a pretty simple issue I just cannot figure it out:

<textarea></textarea>
<button>SHOW X!</button>
<result></result>


n = 0; //x
i = 0;

$('button').live('click', functi开发者_如何学JAVAon () {

    for (i=0;i<=5;i++) {
     $('result').append((($('textarea').val().split("2011")[i]).split("]")[1].split(",")[n].split(" = ")[1]));

    }

});

For some reason when I use an integer in ("2011")[2] instead of "i", it works. Using "i", it fails to work.

http://jsfiddle.net/KRBxp/


T.J. you were right. It works now:

$('button').live('click', function () {

    $('#result').empty();

    var type = $(this).attr('data-type') ;

    var newvalues = $('textarea').val().split('2011');

    for(var i=1; i < newvalues.length; i++) {
        $('#result').append(newvalues[i].split("] ")[1].split(",")[type].split(" = ")[1]+'<br>');
    }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜