最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript- How to pass array as parameter at onclick event? - Stack Overflow

programmeradmin7浏览0评论

I have an array that I want to pass to a javascript onclick function. I send that array in the onclick event of a button. The onclick function return me [object Object]. Is there a different way to do call that function with the array?

Here My Code:

var data_type= $.parseJSON(data_playtype[1]);//{"1":"apple","2":"orange","23":"berry","8":"grape","9":"mango"}


str1.push('<td><a href="javascript:void(0)" onClick="showABC(\''+data_type+'\')">'Data'</td>');



function showABC(fruit){

    $.each(fruit,function(pid,pt){
        alert(pt);
    });

}

I have an array that I want to pass to a javascript onclick function. I send that array in the onclick event of a button. The onclick function return me [object Object]. Is there a different way to do call that function with the array?

Here My Code:

var data_type= $.parseJSON(data_playtype[1]);//{"1":"apple","2":"orange","23":"berry","8":"grape","9":"mango"}


str1.push('<td><a href="javascript:void(0)" onClick="showABC(\''+data_type+'\')">'Data'</td>');



function showABC(fruit){

    $.each(fruit,function(pid,pt){
        alert(pt);
    });

}
Share Improve this question asked Jun 22, 2015 at 2:08 max chengmax cheng 1811 gold badge1 silver badge13 bronze badges 4
  • 2 Don't use HTML and inline event handlers. Use jQuery to build the DOM elements and to bind the event handler, so you can make use of closures. I highly remend to read the jQuery tutorial: learn.jquery./using-jquery-core/manipulating-elements/…, learn.jquery./events/event-basics – Felix Kling Commented Jun 22, 2015 at 2:11
  • pt is an object, so when you do alert(pt), it's stringifying the object to [object Object]. Try to access a specific key of that object. – Josh Beam Commented Jun 22, 2015 at 2:11
  • @JoshBeam: The string conversion already happens earlier when the HTML is built. – Felix Kling Commented Jun 22, 2015 at 2:12
  • @FelixKling, oh yeah, you're right. Talking about in the string'ed onClick, yes? – Josh Beam Commented Jun 22, 2015 at 2:13
Add a ment  | 

2 Answers 2

Reset to default 4

I have an array that I want to pass to a javascript onclick function. I send that array in the onclick event of a button. The onclick function return me [object Object]. Is there a different way to do call that function with the array?

Note, data_type not appear to be Array at js ; data_type appear to be Object


Try utilizing .on() , calling showABC() within click event handler with data_type as parameter.

var data_playtype = [];

data_playtype[1] = JSON.stringify({
  "1": "apple",
  "2": "orange",
  "23": "berry",
  "8": "grape",
  "9": "mango"
});

var data_type = $.parseJSON(data_playtype[1]);
//{"1":"apple","2":"orange","23":"berry","8":"grape","9":"mango"}

var str1 = [];
str1.push("<td><a href=javascript:void(0)>Data</a></td>");

function showABC(fruit) {

  $.each(fruit, function(pid, pt) {
    alert(pt);
  });

};

$("table tbody").append(str1).find("a")
  .on("click", function() {
    showABC(data_type)
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tbody></tbody>
</table>

DEMO

Using inline JS is not a good idea. I would use a data attribute to hold the data as follows:

var data_type = .....;
var link = $('<a/>').data('data_type', data_type).html('Data');
str1.push( $('<td/>').html( link ) );

//event handler
link.on('click', function(e) {
    e.preventDefault();
    var data_type = $(this).data('data_type');
    .....
});

Ref: https://api.jquery./data/

.data( key, value )

key

Type: String A string naming the piece of data to set.

value

Type: Anything The new data value; this can be any Javascript type except undefined.

发布评论

评论列表(0)

  1. 暂无评论