开发者

javascript print array values dynamic

hi everyone i have a problem in javascript i can print array if fix them in html but whn i try to print them on clic they are not working just print the array names if i print seriesre simple it print values that is fine but when i check any checkbox and want to print one or tow of them it just showing array name not values

thanks for help

check this example

$(document).ready(function() {
Comment     =  [['2011-01-29',7695],['2011-02-02',19805]];  
WallPost    =  [['2011-01-29',11115],['2011-02-02',8680]]; 
Likes       =  [['2011-01-29',5405],['2011-02-02',10930]]; 
var seriesre= [Comment,WallPost,Likes];
var mygraphs = new Array(); 
alert(seriesre);
$("#testCheck").click(function() {
    i=0;
    $("#testCheck :checked").each(function() {
        mygraphs[i]= $(this).val();

        i++;
    });
    newseriesre = "["+mygraphs+"]";
    alert(newseriesre);
});
});

<div class="activity">

                 <form method="POST" id="testCheck" name="myform">

                 Likes
                 <input type="checkbox" value="Likes" name="box2">

                 Comments
                 <input type="checkbox"  value="Comment" name="box开发者_StackOverflow中文版3">
                Wall Post
                 <input type="checkbox" value="WallPost" name="box4">
               </form>
            </div>


You can use

alert(myarray.join())

to alert your array's values


You should use a associative array instead of an array, so that you can look up the data based on the name as a string instead of trying to find the variable. All objects in Javascript are associative arrays, so just put the data in an object.

Also:

  • Create the mygraphs array inside the event handler, otherwise it can not shrink when you uncheck options.
  • Catch the click on the checkboxes inside the form, not on the form itself.
  • Put a label tag around the checkbox and it's label, that way the label is also clickable.
  • You don't need an index variable to put values in the mygraphs array, just use the push method to add items to it.

http://jsfiddle.net/cCukJ/

Javascript:

$(function() {
    Comment = [['2011-01-29',7695],['2011-02-02',19805]];  
    WallPost = [['2011-01-29',11115],['2011-02-02',8680]]; 
    Likes = [['2011-01-29',5405],['2011-02-02',10930]]; 
    var seriesre = {
        'Comment': Comment,
        'WallPost': WallPost,
        'Likes': Likes
    };
    $("#testCheck :checkbox").click(function() {
        var mygraphs = [];
         $("#testCheck :checked").each(function() {
            mygraphs.push(seriesre[$(this).val()]);
        });
        alert("["+mygraphs+"]");
    });
});

HTML:

<div class="activity">
  <form method="POST" id="testCheck" name="myform">
    <label>
      Likes
      <input type="checkbox" value="Likes" name="box2">
    </label>
    <label>
      Comments
      <input type="checkbox"  value="Comment" name="box3">
    </label>
    <label>
      Wall Post
      <input type="checkbox" value="WallPost" name="box4">
    </label>
  </form>
</div>


I understand that you want to alert the selected values when clicking anywhere on the form? If that's true correct code with minimal changes to your existing code will be:

var mygraphs = [];
$("#testCheck").click(function() {
    $("#testCheck :checked").each(function() {
        mygraphs.push($(this).val());
    });

    alert("Selected values are: " + mygraphs.join(", "));
});


You can try this.

alert($("#testCheck :checked")
         .map( function(i, field) { return field.value}
         ).get());

Check your working example in http://jsfiddle.net/dharnishr/d37Gn/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜