单击rails 3中的另一个输入字段时,通过js或jQuery添加文本输入字段 [英] adding a text input field through js or jQuery when clicking on another input field in rails 3

查看:78
本文介绍了单击rails 3中的另一个输入字段时,通过js或jQuery添加文本输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Rails 3.1中构建一个嵌套表单,而不是按下添加"按钮,而是要在空的输入字段中键入文本时自动添加输入字段(就像在Facebook中提问和添加投票选项一样) ).但是我也只想添加一个字段,如果删除了这些字符,那么在字段中键入了字符,那么额外的字段也应该删除.

I'm building a nested form in rails 3.1 and instead of pressing the "add" button i want to automaticly add an input field when typing text in an empty input field (just like making a question in facebook and adding poll options). But i also want to only add a field if there are characters typed in the field if the characters are removed the extra field should also be removed.

我正在使用Rails 3.1,因此我的gem中包含了jQuery和jquery-rails,我还不知道咖啡脚本.

I am working with rails 3.1 and so jQuery and jquery-rails are included in my gem, i don't know coffee script yet.

推荐答案

这真的很容易. 只需轻触focus()事件处理程序并在操作过程中添加文本框即可.

it's really easy. Just tap into the focus() event handler and add textboxes as you go along.

我在这里为您创建了一个简单的演示: http://jsfiddle.net/mcVnN/

I have created a simple demo for you here: http://jsfiddle.net/mcVnN/

警告:单击任何文本框将在此处添加一个新的文本框.如果您只想在最后一个文本框焦点上添加新的文本框,只需比较ID.

Warning: clicking any textbox will add a new textbox here. If you want to add new textbox ONLY on last textbox focus, just compare the id.

如果您有任何疑问,请告诉我.

If you have any doubts let me know.

欢呼

- 抱歉,没有读完您的完整问题,我的错. 我在jsfiddle上再次整理了一些快速代码 http://jsfiddle.net/mcVnN/12/ ,但是您可能想要更改它以适合您的需求.

-- Sorry, did not read your full question, my mistake. I have put together some quick code http://jsfiddle.net/mcVnN/12/ again on jsfiddle, but you might want to change it to suit your needs.

这篇关于单击rails 3中的另一个输入字段时,通过js或jQuery添加文本输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆