我有以下非常直接的表格。
<%= form_for(@position) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.autocomplete_field :name, autocomplete_position_name_positions_path %>
</div>
<% end %>
<script type="text/javascript" charset="utf-8">
$('#name').bind('railsAutocomplete.select', function(event, data){
/* Do something here */
alert(data.item.id);
});
</script>
自动完成所有工作都很好,但alert(..)
没有调用该方法。我对所有这些 JavaScript 东西都很陌生,所以如果有人能指出我正确的方向,我将非常感激。编译后的源代码如下所示:
<form accept-charset="UTF-8" action="/positions" class="new_position" id="new_position" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="Sw+3CMXWmEPPYCNeCsAfNRX2DupZzZglMNSAnC4yDV4=" /></div>
<div class="field">
<label for="position_name">Name</label><br />
<input data-autocomplete="/positions/autocomplete_position_name" id="position_name" name="position[name]" size="30" type="text" />
</div>
</form>
<script type="text/javascript" charset="utf-8">
$('#name').bind('railsAutocomplete.select', function(event, data){
/* Do something here */
alert(data.item.id);
});
</script>
非常感谢,伙计们!