2012-11-18 58 views
-1

我想用jquery selectable替換我的醜陋複選框。我將此代碼添加到了我的form_for中,但我不知道如何將tag_list數組傳回給我的控制器。傳遞一個JavaScript數組到軌道控制器

<meta charset="utf-8" /> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css" /> 

    <style> 
    #feedback { font-size: 1.4em; } 
    #selectable .ui-selecting { background: #FECA40; } 
    #selectable .ui-selected { background: #F39814; color: white; } 
    #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; } 
    #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; } 
    </style> 
    <script> 
    $(function() { 
     $("#selectable").selectable({ 
      stop: function() { 
       var result = $("#select-result").empty(); 
       var tag_names = [] 
       $(".ui-selected", this).each(function() { 
        var item = this.innerHTML; 
        result.append((item)); 
        tag_names.push(item) 
       }); 
      } 
     }); 
    }); 
    </script> 
</head> 
<body> 

<p id="feedback"> 
<span>Tags:</span> <span id="select-result">none</span>. 
</p> 

<ol id="selectable"> 
    <%@tags.each_key do |tag| %> 
     <li class="ui-widget-content"> #<%=tag%> </li> 
    <%end%> 
</ol> 
+0

如果你覺得我的回答是有用的,請給予好評,並接受它。 –

回答

0

兩種方式,我可以看到:

  1. 首先,在表單中創建一個隱藏字段。並通過Jquery通過tag_names填充它的值。它會在表單提交時自動提交。
  2. 其次,你可以創建一個GLOBAL JS變量tag_names。它將像上面那樣被填充,然後覆蓋Jquery中的表單提交操作,以包含tag_names。就像這樣:

    $("#myForm").submit(function() { 
        $(this).append("<input type='hidden' name='tag_names' 
        value='"+tag_names+"' />"); 
        $(this).submit(); 
    }) 
    
相關問題