简体   繁体   English

如何将javascript数组传递给onclick =“('here')”?

[英]How can I pass a javascript array to onclick=“('here')”?

I have a program that requires I continuously send the array back and forth from the screen to the server. 我有一个程序,要求我不断地从屏幕上来回发送数组到服务器。 The array is important, so I need it. 阵列很重要,所以我需要它。

So it starts as a php array in an onclick -> it goes to javascript and calls the php function again which then returns it back to the javascript function. 因此它在onclick中作为一个php数组开始 - >它转到javascript并再次调用php函数然后将其返回到javascript函数。 This is where I'm lost. 这就是我失去的地方。 After the php array is returned, I'm trying to put it back into the onclick so that when the user clicks again, the new array is send with the onclick. 返回php数组后,我试图将其重新放入onclick中,以便当用户再次单击时,将使用onclick发送新数组。

How can I go about doing this? 我该怎么做呢?

What i have so far: 到目前为止我有什么:

Within the javascript, after a success message is received and the PHP function delivers the new array I have something like this set up where msg is holding the values. 在javascript中,在收到成功消息并且PHP函数传递新数组之后,我有类似这样的设置,其中msg保存值。 msg.question_id is holding just an integer, msg.array is holding a php array. msg.question_id只保存一个整数,msg.array持有一个php数组。

document.getElementById('buttons').innerHTML ="<a href='javascript:void(0)' onclick=\"movie_guess('yes',"+msg.question_id+","+msg.array+")\"><img  src=\"/assets/yes.png\" ></a><a href=\"javascript:void(0)\" onclick=\"movie_guess('no',"+msg.question_id+","+msg.array+")\"><img  src=\"/assets/no.png\" ></a> ";

Well I guess you should not return PHP array in ajax response. 好吧,我想你不应该在ajax响应中返回PHP数组。 You can send it as a string with a delimiter. 您可以将其作为带分隔符的字符串发送。 On successful response you can then split those and create a javascript array. 成功响应后,您可以拆分它们并创建一个javascript数组。 You can also using JSON. 您也可以使用JSON。 Encode it to access it. 对其进行编码以访问它。

$phpArr=( 0 => 'Zero', 1 => 'One', 2 => 'Two'); // PHP Code
var jsArr= <?php echo json_encode($phpArr); ?>; // Javascript Code
for($i=0;<stop-condition>;$i++){      alert(jsArr[i]);  } // Javascript Code

This is the element you're trying to add, properly indented: 这是您尝试添加的元素,正确缩进:

<a href='javascript:void(0)' onclick=\"movie_guess('yes',"+msg.question_id+","+msg.array+")\">
  <img  src=\"/assets/yes.png\" >
</a>
<a href=\"javascript:void(0)\" onclick=\"movie_guess('no',"+msg.question_id+","+msg.array+")\">
  <img  src=\"/assets/no.png\" >
</a>

Instead, you could add it without the handlers (I have also used # as the link target; also note how I avoided the need to escape quotes and how you can split long strings to several lines): 相反,你可以在没有处理程序的情况下添加它(我也使用#作为链接目标;还要注意我如何避免需要转义引号以及如何将长字符串拆分为多行):

var buttons = document.getElementById('buttons');
buttons.innerHTML = 
  '<a href="#"><img src="/assets/yes.png"></a>'+
  '<a href="#"><img src="/assets/no.png"></a>'

then add the event handlers as functions. 然后将事件处理程序添加为函数。 This improves performance and lets you pass anything present visible from the current scope (I'm assuming msg is a javascript object and that it doesn't change). 这样可以提高性能,并允许您传递当前作用域中可见的任何内容(我假设msg是一个javascript对象并且它不会更改)。 addEventListener has other benefits over onclick=function(){...} as well: addEventListener还有onclick=function(){...}其他好处:

var links = buttons.getElementsByTagName("a");
links[0].addEventListener("click", function(){
  movie_guess('yes', mgs.question_id, msg.array);
});
links[1].addEventListener("click", function(){
  movie_guess('no', mgs.question_id, msg.array);
});

A longer but faster version would be to not touch innerHTML at all, and manipulate the DOM instead: 更长但更快的版本是根本不触摸innerHTML ,而是操纵DOM:

var buttons = document.getElementById('buttons');
var aYes = document.createElement("a");
aYes.href = "#";
aYes.addEventListener(...);
var imgYes = document.createElement("img");
imgYes.src = "/assets/yes.png";
aYes.appendChild(imgYes);
buttons.appendChild(aYes);
var aNo = document.createElement("a");
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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