簡體   English   中英

select_tag和javascript在Rails問題中

[英]select_tag and javascript in rails issue

我試圖使用rails select_tag生成html並將其保存到要動態使用的變量中。 由於某些原因,以下是導致此錯誤的原因:

未捕獲的語法錯誤:意外的令牌非法

下面是問題代碼:

function addItemRowHTML(offsetidentifiervalue)
{
    $node = ' \
         <%= select_tag "currency",  
            options_for_select(Country.all.each_with_index.map {
            |country, index| 
                [country["currency_code"],
                 country["currency_code"]]},
                @invoice["data"]["currency"] ),
                :style => "width:120px"
        %> \
        ';

}

雖然text_field_tag有效

$node =' <%= text_field_tag("amount", "10") %> ';

那我該如何處理select_tag? 謝謝

<% html_select_tag = select_tag( "currency",  
      options_for_select(Country.all.map(&:currency_code),
      @invoice["data"]["currency"] ),
      :style => "width:120px" ).gsub("\n", "\\n").gsub("'","\\'") %>

var $node = '<%= html_select_tag %>';

有人已經在這里回答了類似的問題: https : //stackoverflow.com/a/13631186/2134720

這有效

    $node = '<% select_tag("currency",  
            options_for_select(Country.all.each_with_index.map {
            |country, index| 
                [country["currency_code"],
                 country["currency_code"]]},
                @invoice["data"]["currency"] ),
                :style => "width:120px").gsub("\n", "\\n").gsub("'","\\'")
    %> ';

這將是一個更好的解決方案,使用raw可以避免換行符的減少

    $node = '<% raw select_tag("currency",  
            options_for_select(Country.all.each_with_index.map {
            |country, index| 
                [country["currency_code"],
                 country["currency_code"]]},
                @invoice["data"]["currency"] ),
                :style => "width:120px").gsub("\n", "\\n").gsub("'","\\'")
    %> ';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM