简体   繁体   English

Rails视图中的JQuery append()提供“未捕获的SyntaxError:无效或意外的令牌”

[英]JQuery append() in Rails view gives “Uncaught SyntaxError: Invalid or unexpected token”

I'm trying to dynamically add/remove select boxes to my form on the click of a button 我正在尝试通过单击按钮来向表单中动态添加/删除选择框

Here is my options/new.html.erb : 这是我的options/new.html.erb

<br>
  <h3>Add Options</h3>
  <div class="input_options_wrap">
    <button class="add_options_button">+</button>
    <%=select_tag "options[]", options_for_select(@options.pluck(:option)), class: "form-control", prompt: "Select Option" %>
  </div>

This shows one line with the select box and the options. 这显示了带有选择框和选项的一行。 I want the user to be able to select as many options as he wants to add, but they need to be from this list of options only. 我希望用户能够选择要添加的尽可能多的选项,但是它们只需要从此选项列表中选择即可。 So, there is the add_button and here is the script to handle the add/remove select boxes on click: 因此,有一个add_button,这是处理单击时添加/删除选择框的脚本:

<script>
  $(document).ready(function () {
    var max_fields = 10; //maximum input boxes allowed
    var wrapper = $(".input_options_wrap"); //Fields wrapper
    var add_button = $(".add_options_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function (e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><%=select_tag "options[]", options_for_select(@options.pluck(:option)), class: "form-control", prompt: "Select Option" %><a href="#" class="remove_field">Remove</a></div>');
        }
    });

    $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })
  })
</script>

Now here is the problem. 现在这是问题所在。 On rendering this html page, I am getting Uncaught SyntaxError: Invalid or unexpected token . 在呈现此html页面时,我收到了Uncaught SyntaxError: Invalid or unexpected token Here is how the above code (relevant line) is being parsed: 这是上面的代码(相关行)的解析方式:

$(wrapper).append('<div><select class="form-control" id="options_" name="options[]"><option value="">Select Option</option><option value="book">book</option>
<option value="copy">copy</option></select>
</div>');

I believe this exception is because inside append , it expects one string of "html code" while here because of random line breaks, that single string is no more syntactically correct. 我相信这种异常是因为在append ,它期望一个字符串“ html代码”,而在这里由于随机换行,单个字符串在语法上不再正确。 Is there any way around this? 有没有办法解决?

You could put that html into a script tag to use as template and retrieve it as html from that template. 您可以将该html放入脚本标签中以用作模板,然后从该模板中将其检索为html。 By setting the type to something other than javascript the browser won't compile it as script and the html inside won't be turned into dom nodes either. 通过将type设置为非javascript的浏览器,浏览器将不会将其编译为脚本,并且内部的html也不会变成dom节点。

<script type="text/template" id="select-template">
  <div><%=select_tag ..... %><a..></div>
</script>

Then change: 然后更改:

$(wrapper).append('html string')

To: 至:

$(wrapper).append($('#select-template').html());

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

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