简体   繁体   中英

how to pass array values to javascript function from html onchange action?

I have a separate array like

1 1 1
2 2 2
3 3 3

I need to pass this array to JavaScript function as a parameter while onChange action is called.

can anyone give me a suggestion for that?

Thanks.

Try something like below

var x1=[1,11,11];
var x2=[2,21,22];
var x3=[3,31,32];
var y=[x1,x2,x3];
onChange(y);

Don't know if that's an option, but this is how it may work in jQuery:

HTML:

<textarea id="target" />

jQuery:

$(document).ready(function(){
    $('#target').change(function() {
      var arr1 = [1,2,3];
      var arr2 = [1,2,3];
      var arr3 = [1,2,3];
      var arr = [arr1, arr2, arr3];
      //do whatever you want with it
      alert(arr);
});
});

I guess you could assign it to some hidden element (like input hidden) and then write some code that would read the arr value from it.

At least that's one of options - I'm not sure what you're trying to achieve.

Hope this helps

You need to wrap the function call in another anonymous function.

element.addEventListener('change', function(e){
    yourFunction(yourArray);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM