我想创建一个包含当前现有品牌(例如 Sony、Panasonic 等)的 select
框。此外,我希望有 Add New Brand
选项,这样当用户单击此选项时,会出现一个新的文本字段。
Rails 3 中是否有任何辅助方法可以做这样的事情,或者我需要自己使用 Javascript 实现它?
最佳答案
据我所知,没有这样的辅助方法。
这是我在 JS 中的做法:
document.getElementById('someSelectBox').onchange = function() {
if(this.selectedIndex != this.options.length -1) return;
var new_name = prompt('Please enter a name');
if(!new_name.length) return;
var textbox = document.createElement('input');
textbox.value = new_name;
this.parentNode.appendChild(textbox); //parentNode is presumably the form
}
关于javascript - Rails 3 表单问题 : How to create a "select" with "Add New ..." option?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4419082/