English 中文(简体)
"Expected ]" error while passing an object as parameter to js function
原标题:

I am trying to pass an object as parameter to the javascript function as:

    .html( <span class="link" onclick="javascript:ShowEventDetails(  + event +  ,  
    + event.Id +  ,  + event.Name +  ,  + view +  )">  + event.title 
+  </span> )

In the above js, the event and view are objects from the jquery calendar Full Calendar, which i am passing to call a js function.

It throws an error, Expected ] . What may be problem or how to pass an object as above?

It shows "javascript:ShowEditEventDetails([object Object],1, Name ,[object Object])" while looking in firebug.

最佳回答

You can t pass an object in that way, because when you concatenate the HTML string of your span element, all the operands of the + operator are converted toString, including the event object, that s why you get "[object Object]":

   + {} == "[object Object]"; // an empty object

I would recommend you to bind the click event programmatically:

// ...
var $span = $( <span class="link">  + event.title +  </span> );
$span.click(function () {
  ShowEventDetails(event, event.Id, event.Name, view);
});

$( #otherElement ).append($span);
// ...
问题回答

That s because ShowEventDetails isn t being passed event, it s being passed String(event), which is [object Object], which yields a syntax error. Assuming event and view are global variables, use this instead:

.html( <span class="link" onclick="javascript:ShowEventDetails(event,  
      + event.Id +  ,  + event.Name +  ,view)">  + event.title 
      +  </span> )

You shouldn t be using .html for such a thing though. This solution is better and doesn t require global variables:

.addClass("link")
.click(function () {
  ShowEventDetails(event, event.Id, event.Name, view)
})
.text(event.title);

What s Going Wrong

event and view are both objects. When you concatenate an object to a string (as you re doing with ...ShowEventDetails( + event + , ...), JavaScript casts the object to a string, resulting in the nearly useless [object Object] text you see in FireBug. When the JavaScript interpreter later tries to parse [object Object] as code, it blows up because it is invalid syntax.

How to Fix It

The easiest thing to do is set your event handler programmatically:

var mySpan = $( <span class="link" /> );
mySpan.text(event.title);
mySpan.click(function (ev) {
  ShowEventDetails(event, event.Id, event.Name, view);
});

parentElement.append(mySpan);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签