I've got a form with a dropdown (%ul.dropdown
) and a text field (%input
). The dropdown has a list of books. Whenever an item from the dropdown is clicked, the book title will appear in the text field (I do this in a Javascript not included in the code sample). If I submit the form, I want the id from the item to be submitted, not the title itself. I can achieve this by messing around with hidden fields and jQuery, however, it would be better if there was a simple and elegant way to do it. Any thoughts? Thanks
我有一个带有下拉列表(%ul.dropdown)和文本字段(%输入)的表单。下拉列表中有一系列书籍。每当点击下拉列表中的项目时,书名将出现在文本字段中(我在代码示例中未包含的Javascript中执行此操作)。如果我提交表单,我希望提交项目的ID,而不是标题本身。我可以通过搞乱隐藏字段和jQuery来实现这一点,但是,如果有一种简单而优雅的方法可以做到这一点会更好。有什么想法吗?谢谢
Code (in haml):
代码(以haml为单位):
.input-group
%input.form-control#book-selected{ type: "text", placeholder: "Please, select" }/
.input-group-btn
%button.btn.button-transparent.dropdown-toggle{"data-toggle" => "dropdown", type: "button"}
.icon-arrow-down
%ul.dropdown-menu.pull-right#books-list
- @books.each do |book|
%li
%a.book-select{ href: "#", 'data-book-id' => book.id }= book.name
1 个解决方案
#1
0
Here are some examples of submit listeners:
以下是提交侦听器的一些示例:
Submit Event Listener for a form
提交表单的事件监听器
Use submit event listener when form is submitted via inline JavaScript?
通过内联JavaScript提交表单时使用提交事件监听器?
A similar question has been asked here.
这里也提出了类似的问题。
Code example:
$('#myform').submit(function(event) {
$('#book-selected').val( currentBookId );
this.submit();
});
#1
0
Here are some examples of submit listeners:
以下是提交侦听器的一些示例:
Submit Event Listener for a form
提交表单的事件监听器
Use submit event listener when form is submitted via inline JavaScript?
通过内联JavaScript提交表单时使用提交事件监听器?
A similar question has been asked here.
这里也提出了类似的问题。
Code example:
$('#myform').submit(function(event) {
$('#book-selected').val( currentBookId );
this.submit();
});