开发者

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

开发者 https://www.devze.com 2023-03-20 11:43 出处:网络
I have a pretty simple issue I just cannot figure it out: <textarea></textarea> <button>SHOW X!</button>

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

精彩评论

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