简体   繁体   中英

Uncaught SyntaxError: Invalid regular expression flags — what is going on?

I am trying to create rows dynamically that when clicked load a view for the associated row. My code (in javascript and jquery) is below

    var row = $('<tr />');
    var action = '@Url.Action("Get", "myController", new { myID = "__param__" })';
    action = action.replace('__param__', rowData.myID)
    row.attr('onclick', action);

    $("#ListTable > tbody").append(row);

The rows created look like this

<tr onclick="/myControler/Get?myID=113066"><td width="20%">12345</td><td width="80%">Test Text</td></tr>

However, when I click the row, I get the error:

Uncaught SyntaxError: Invalid regular expression flags.

I have no clue what I'm doing wrong here, as everything looks good. Any help would be appreciated.

You should not be using attr() to attach an event. What you want to do is make a link when they click? So you need to set the page location.

row.on('click', function () {
  window.location.href = action
});

Your onclick attribute must contain JavaScript. In JavaScript, an expression that starts and ends with slashes, like /myControler/ is a regular expression.

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