jQuery 可以为 .class 的未来成员添加插件函数吗?
我不知道这是否可能,但这是我的问题:
我的 jQuery 应用程序动态添加了输入字段。例如,“添加新客户”按钮在当前表单之后添加一个新的输入表单。到目前为止,一切都很好!
但是,我发现自己需要将我想要的掩码函数添加到每个新创建的字段中,如下所示:
$("#customer_zipcode\\["+customerCount+"\\]).zipcodeMask(....);
zipcodeMask(...)
只是一个示例函数,用于检查以确保用户输入了正确的值邮政编码。其他类似的电话号码、信用卡等掩码。这部分效果很好,但我必须为每个新创建的元素设置它,我想要有一个邮政编码掩码。
我认为如果有像 .live()
这样的东西可以为邮政编码字段的每个未来实例执行 .zipcodeMask()
调用,那就太棒了。
我想我应该创建一个类,例如 "UseZipCodeMask"
,并在每个需要邮政编码掩码的输入字段中执行类似 class="UseZipCodeMask"
的操作。
当我这样做时:
$(".UseZipCodeMask").zipcodeMask(....);
这对于具有此类的每个现有元素来说效果很好。如果我使用此类创建一个新字段,则它的 .zipcodeMask(...)
函数无法正常工作。
有没有办法让我的邮政编码字段自动设置 .zipcodeMask(...)
?它会稍微清理一下代码,每次创建一些新的输入字段时都会添加很多代码 .zipcodeMask()
和其他相关函数。
非常感谢!
Here's something I don't know if it's possible, but here's my question:
My jQuery application has dynamically added entry fields. For example, a button "Add New Customer" adds a new entry form after the current one. So far, so good!
However, I find myself needing to add the mask functions I want to every newly created field, like this:
$("#customer_zipcode\\["+customerCount+"\\]).zipcodeMask(....);
zipcodeMask(...)
is just an example function that checks to make sure the user's entered a correct zip code. Other similar masks for phone numbers, credit cards, etc. This part works great, but I have to set it for every newly created element I want to have a zip code mask.
I thought it would be awesome if there was something like .live()
that could do the .zipcodeMask()
call for every future instance of the zip code field.
I thought I'd make a class, such as "UseZipCodeMask"
, and do something like class="UseZipCodeMask"
in every input field that needs a zip code mask.
When I do this:
$(".UseZipCodeMask").zipcodeMask(....);
This works fine - for every existing element that has this class. If I create a new field with this class, it's doesn't have the .zipcodeMask(...)
function working.
Is there a way to make my zip code fields get .zipcodeMask(...)
automatically set for it? It would clean up the code a bit, there's a lot of code that adds .zipcodeMask()
and other related functions every time some new input fields are created.
Thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你使用什么jquery版本?
您可以使用 .delegate 这意味着您不必将插件“添加”到新创建的元素中。
What jquery version are you using?
You can use .delegate which means you would not have to "add the plugin" to newly created elements.