简体   繁体   English

在 setAttribute onClick 中添加 forEach 元素时缺少 ( 在参数列表之后

[英]Missing ( after argument list when forEach element is added in a setAttribute onClick

I'm trying to set an onclick attribute triggering a function and pass parameters in that function.我正在尝试设置触发 function 的 onclick 属性并在该 function 中传递参数。 Some parameters come directly from the element of my array in which I'm generating the button in which I want create the new attribute on click.一些参数直接来自我要在其中生成按钮的数组元素,在该按钮中我想在单击时创建新属性。 As soon as I use an element attribute (eg element.startTime), I get the error "Uncaught SyntaxError: missing ) after argument list".一旦我使用元素属性(例如 element.startTime),我就会在参数列表之后收到错误“Uncaught SyntaxError:missing”。

What am I doing wrong?我究竟做错了什么?

Here is my code:这是我的代码:

if(days[day]) {
                days[day].forEach(
                    (element) => {
                        var scheduleChild = document.createElement('div')
                        scheduleChild.className = 'btn btn-link text-center schedulesTime'
                        var address = document.createAttribute("data-address")
                        address.value = element.address
                        scheduleChild.setAttributeNode(address)
                        var startTime = document.createAttribute("data-startTime")
                        startTime.value = element.startTime
                        scheduleChild.setAttributeNode(startTime)
                        var endTime = document.createAttribute("data-endTime")
                        endTime.value = element.endTime
                        scheduleChild.setAttributeNode(endTime)
                        scheduleChild.innerText = element.startTime+'-'+element.endTime
                        scheduleChild.setAttribute('onClick','bookTimeslot('+id_provider+','+element.startTime+')')
                        dayParent.appendChild(scheduleChild)

                    }
                )}

The creation of the button is working and I can see the onclick attribute set in my button.按钮的创建正在工作,我可以看到我的按钮中设置的 onclick 属性。 Here as example, the HTML of a button I create:这里以我创建的按钮的 HTML 为例:

<div class="btn btn-link text-center schedulesTime" data-address="3" data-starttime="09:00" data-endtime="09:45" onclick="bookTimeslot(3,09:00)">09:00-09:45</div>

Don't try to build code strings like that.不要尝试构建这样的代码字符串。 Instead, assign an actual function (or better yet, use addEventListener ):相反,分配一个实际的 function (或者更好的是,使用addEventListener ):

scheduleChild.addEventListener("click", () => bookTimeslot(id_provider, element.startTime));

The most likely issue in this specific case is the lack of quotes around the date string, but again, building code strings is error-prone and unnecessary.在这种特定情况下,最可能的问题是日期字符串周围缺少引号,但同样,构建代码字符串容易出错且不必要。

Note: That will use the value of element.startTime as of when the click occurs.注意:这将使用单击发生时element.startTime的值。 If it's possible that element may change, and you want the previous value to be used, grab it locally:如果该元素可能会更改,并且您希望使用以前的值,请在本地获取它:

const startTime = new Date(+element.startTime); // If it's a `Date`
scheduleChild.addEventListener("click", () => bookTimeslot(id_provider, startTime));

The error is because of the value of startTime, since it's value contains : , so you shoud put it inside an "" .错误是因为 startTime 的值,因为它的值包含: ,所以你应该把它放在一个""中。 like:喜欢:

scheduleChild.setAttribute('onClick','bookTimeslot('+id_provider+',"'+element.startTime+'")')

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

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