简体   繁体   English

我可以通过 object 作为 onClick function 的参数吗

[英]Can I pass an object as the parameter for an onClick function

I am currently trying to create an onclick event for a button that calls a separate function and passes uses an object as the parameter for this function. I am currently trying to create an onclick event for a button that calls a separate function and passes uses an object as the parameter for this function.

My code can be seen below:我的代码如下所示:

async function getJourneyAwait(){
  const routes = await getJourney();
  var innerHTML = " "; 
  if(!(routes === null)) {
    for (var i = 0; i < routes.length; i++){
        console.log(routes[i])
        console.log(typeof(routes[i]))
        var route = routes[i]
        innerHTML += '<p> Route ' + i+1 + ': <button onClick=startJourney(' + route + ')>Start Trip</button></p>'
    }
    document.getElementById('tripmessage').innerHTML = innerHTML;
  }
}


function startJourney(route){

    console.log(route);
}

When I try to click the Start Trip button I get an error stating: Uncaught SyntaxError: Unexpected end of input at.(index):1当我尝试单击 Start Trip 按钮时,我收到一条错误消息:Uncaught SyntaxError: Unexpected end of input at.(index):1

When I inspect the button element it there seems to be some kind of error with the parameter as the element is as follows:当我检查按钮元素时,参数似乎存在某种错误,因为元素如下:

   
<button onclick="startJourney([object" object])="">Start Trip</button>

I have tried multiple different ways and in some cases I have been able to get the function to run but when I do all that is logged to the console is undefined.我尝试了多种不同的方法,在某些情况下,我能够让 function 运行,但是当我执行所有记录到控制台的操作时,这些都是未定义的。 For example, if I remove the plus signs and quotes on either side of 'route' the function runs but undefined is logged to the console.例如,如果我删除“路由”两侧的加号和引号,function 会运行,但 undefined 会记录到控制台。

The [object object] that you are seeing is the result of the conversion of your route object to a string format.您看到的[object object]是将您的route object 转换为字符串格式的结果。

I suggest one of two approaches:我建议以下两种方法之一:

  1. Create the button element using createElement , assign the startJourney function to the onclick property using button.onclick = function() { createJourney(route) ]} and append it to the parent element. Create the button element using createElement , assign the startJourney function to the onclick property using button.onclick = function() { createJourney(route) ]} and append it to the parent element.
  2. Add an id to the button element and add a click event listener using addEventListener("click", createJourney(route))向按钮元素添加 id 并使用addEventListener("click", createJourney(route))添加点击事件监听器

Edit: As pointed out by @Teemu, in case you use option #1, you'll have to replace var i = 0 by let i = 0 since in a loop with a let-based index, each iteration through the loop will have a new variable i with loop scope.编辑:正如@Teemu 所指出的,如果您使用选项#1,则必须将var i = 0替换为let i = 0因为在具有基于 let 的索引的循环中,通过循环的每次迭代都将具有带有循环 scope 的新变量 i。

You should I would recommend you use Data attributes and also bind your buttons this way:您应该建议您使用Data 属性并以这种方式绑定您的按钮:

 function getJourneyAwait(){ const routes = getJourney(); var innerHTML = " "; if(routes) { for (var i = 0; i < routes.length; i++){ var route = routes[i] innerHTML += '<p> Route ' + route + ': <button onclick="startJourney(this)" data-route="' + route + '">Start Trip</button></p>' } document.getElementById('tripmessage').innerHTML = innerHTML; } } function getJourney(){ return [1, 2, 4, 6] } function startJourney(element){ console.log(element.dataset.route); }
 <div id="tripmessage"></div> <button onclick="getJourneyAwait()">GetJourney</button>

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

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