简体   繁体   English

jQuery动态添加表单字段问题

[英]Jquery dynamic addition of form fields problem

I'm using the following code to insert extra form fields. 我正在使用以下代码插入额外的表单字段。

function addFormField() {
$("#divTxt").append("<div id='education" + id + "'><input name='name' id='name' type='text' size='20'><a href='#' onClick='removeFormField(\"#education" + id + "\"); return false;'><img src='images/minus.gif' width='10px' border=0></img></a></div>");
}

I'm dynamically sending the field values to mysql when a blur event occurs. 当发生模糊事件时,我会将字段值动态发送到mysql。 However, when this field is inserted, it doesn't recognise and the blur event isn't picking up when any value has been entered on the new fields. 但是,插入该字段时,如果在新字段中输入了任何值,则无法识别并且模糊事件也不会发生。 Is this due to the original blur event handler being set up on document ready? 这是由于原始模糊事件处理程序已在文档上准备好了吗?

How do I get the mysql update jquery code to recognise when the extra form fields are made visible after the document ready initialisation has already been completed? 在准备好文档的初始化完成后,如何使额外的表单字段可见,如何获取mysql更新jquery代码? I've tried various events based on the div id but to no avail..... 我已经尝试过基于div id的各种事件,但无济于事。

The reason your code is not working for the dynamically added inputs is because when you do something like: 您的代码不适用于动态添加的输入的原因是,当您执行以下操作时:

$(selector).blur(myFunction);

jQuery goes through every element that matches selector at that point and adds an event handler that runs myFunction when the blur event is fired happens on the element. jQuery的经历每个匹配元素selector 在这一点上 ,并增加了运行的事件处理程序myFunctionblur事件被触发的元素发生。 This means that any elements that match selector added after this line of code runs will not have been bound. 这意味着此行代码运行之后添加的与selector匹配的任何元素都不会被绑定。

To get around this problem, jQuery introduced the live function in 1.3. 为了解决这个问题,jQuery在1.3中引入了live函数。 As the documentation reads: 正如文档所示:

Binds a handler to an event (like click) for all current - and future - matched element. 将所有当前-和将来-匹配元素的处理程序绑定到事件(例如click)。 Can also bind custom events. 也可以绑定自定义事件。

Unfortunately, as of right now jQuery does not support the blur event with the live function. 不幸的是,到目前为止,jQuery不支持带live函数的blur事件。

Your options then are: 然后,您的选择是:

A) Run the binding code everytime you add new inputs. A)每次添加新输入时都运行绑定代码。
B) Use the livequery plugin, which is that live is based off of and does support blur. B)使用livequery插件,该插件基于live并支持模糊。

Personally, I would go with A. 就个人而言,我会选择A。

You should bind your events using the live() method: 您应该使用live()方法绑定事件:

for example: 例如:

$("input").live("blur", function() { ... });

That way, any fields added at runtime will be bound to the event handler. 这样,在运行时添加的任何字段都将绑定到事件处理程序。

EDIT: as pointed out in the comments, "blur" is not supported, but there's a plugin that does support this event: http://plugins.jquery.com/project/livequery 编辑:如注释中指出,不支持“模糊”,但是有一个插件确实支持此事件: http : //plugins.jquery.com/project/livequery

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

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