简体   繁体   English

我如何将Rails form_helper标签附加到html

[英]How can i append rails form_helper tag to html

I have to append the rails select_tag code after the click event, 我必须在click事件之后附加rails select_tag代码,

this is the code, 这是代码,

var result = '<%=select_tag("repeat_time", options_for_select(get_repeat_time_list_daily, "1")) %>';
$("#repeat_time_area").html(result);

and it has this error, 它有这个错误,

Uncaught SyntaxError: Unexpected token < 

and this is the html parsed code, 这是html解析的代码,

var result = '<select id="repeat_time" name="repeat_time"><option value="1" selected="selected">1일</option>
<option value="2">2일</option>
<option value="3">3일</option>
<option value="4">4일</option>
</select>'

as you can see, rails code automatically makes a new line and that cause the problem. 如您所见,Rails代码会自动换行并引起问题。

var result = '<select id="repeat_time" name="repeat_time"><option value="1" selected="selected">1일</option><option value="2">2일</option><option value="3">3일</option> <option value="4">4일</option></select>'

this is my expected way. 这是我的预期方式。

I tried all kind of things to make this as a straight string line without new line 我尝试了各种方法使它成为straight string line without new linestraight string line without new line

.gsub, h, html_safe, gstrip

but all this kind of libs are not working :( 但是所有这些库都不起作用:(

any good solution?? 任何好的解决方案?

Use escape_javascript or alias j() helper to escape carriage returns, single and double quotes for JS arguments as: 使用escape_javascript或别名j()辅助escape_javascript将回车符,JS参数的单引号和双引号转义为:

var result = '<%= j select_tag("repeat_time", options_for_select(get_repeat_time_list_daily, "1")) %>';
$("#repeat_time_area").html(result);

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

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