2012-11-18 55 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(); 
    }) 
    
相关问题