最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Parentheses in string of javascript function call - Stack Overflow

programmeradmin5浏览0评论

I have a loop that creates links with a javascript function call in the onClick events and uses the text returned from a database as one of the parameters. My issues I am having is that sometimes this text being returned has parenthesis in them which is causing a syntax error in my code. Example:

code:
formResults += "<a onclick='openForm(" + this.displayText + "," + this.ID + ");'>" + this.displayText + "</a>";

HTMLDisplay:
<a onclick="openForm(Example Form (Example Form 1) Application Instructions ,1108);">Example Form (Example Form 1) Application Instructions </a>

as you can see the name of the form contains a set of parenthesis. Is there anyway I can include these? The reason I need to is because the function points to another system that uses the ID and displayText in order to render the proper form. thank you

I have a loop that creates links with a javascript function call in the onClick events and uses the text returned from a database as one of the parameters. My issues I am having is that sometimes this text being returned has parenthesis in them which is causing a syntax error in my code. Example:

code:
formResults += "<a onclick='openForm(" + this.displayText + "," + this.ID + ");'>" + this.displayText + "</a>";

HTMLDisplay:
<a onclick="openForm(Example Form (Example Form 1) Application Instructions ,1108);">Example Form (Example Form 1) Application Instructions </a>

as you can see the name of the form contains a set of parenthesis. Is there anyway I can include these? The reason I need to is because the function points to another system that uses the ID and displayText in order to render the proper form. thank you

Share Improve this question asked Jun 1, 2015 at 16:07 Nick GNick G 1,2398 gold badges35 silver badges60 bronze badges 2
  • 3 Don't do that. Use addEventListener – SLaks Commented Jun 1, 2015 at 16:10
  • If you give us a wider context here for what this code is doing, we might be able to suggest better solutions that don't require the construction of plicated strings that are a bination of HTML and Javascript which is generally a mess to read (and as you have discovered) a mess to write. – jfriend00 Commented Jun 1, 2015 at 16:20
Add a ment  | 

2 Answers 2

Reset to default 4

The parenthesis aren't the problem, it's the lack of quotes inside the function.

formResults += "<a onclick='openForm(\'" + this.displayText + "," + this.ID + "\');'>" + this.displayText + "</a>";

This below snippet (from yours)

`openForm(Example Form...`)

Will throw an error because it's looking for variables Example and so on, quote that string!

I strongly suggest

code:

formResults += '<a class="openForm" data-text="'+this.displayText + '" id="'+this.ID + '">' + this.displayText + '</a>';

HTMLDisplay:

<a class="openForm" data-text="Example Form (Example Form 1) Application Instructions" id="1108">Example Form (Example Form 1) Application Instructions </a>

jQuery:

$(function() {
  $(".openForm").on("click",function(e) {
    e.preventDefault();
    openForm($(this).data("text"),this.id);
  });
});
发布评论

评论列表(0)

  1. 暂无评论