繁体   English   中英

从下拉菜单中选择或添加另一个

[英]Select from drop down menu or add another

我正在使用simple_form。 如何拥有选择菜单和文本输入,以及如何Select or add another

该应用程序只会从选择菜单或文本输入中获取一个值。

验证是否拥有一个或另一个而不是两者,以避免用户混淆也将是一件好事。

在您的simple_form中实现所谓的“组合框”

jQuery UI有一个组合框: http : //jqueryui.com/autocomplete/#combobox

更有趣的东西: http : //demos.kendoui.c​​om/web/combobox/index.html

这将使您尽可能地显示您的组合框。 我认为没有用于验证的插件,因此您必须自己编写代码。

您可以尝试结合使用JavaScript和Ruby来解决此问题。 如果用户想输入一个不同的值,然后在下拉列表中可用,那么您应该让JS监听该输入中的keydown事件并清除下拉列表,即:

$(".input_field").bind('keydown', function() {
  $(".select_field").val('0') // this should be a default blank value
});

用户键入时将清除选择。 同样,您想在用户从下拉菜单中选择时清除输入,对吗?

$(".select_field").change(function() {
  // Let's only clear the input field if the value is not the default null value
  if ( $(this).val() != 0 ) {
    $(".input_field").val('');
  }
});

这将处理前端交互。 在控制器中,您需要执行一些其他逻辑:

def create
  object.new(...)
  if params[:select] and params[:input]
    # Overwrite the select with the input value
    object.attribute = params[:input]
  end
  object.save
end

我假设您希望输入值替代选择,因此,如果它们是提交最多的选择,我们可以用输入值覆盖选择值,然后继续保存对象。

这是您要找的东西吗? 不知道输入的值是否应该创建具有关系的新对象。 希望这可以帮助。

暂无
暂无

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

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