简体   繁体   English

动态添加输入字段的键盘功能

[英]keyup function for dynamically added input fields

I trying to implement keyup function for dynamically added input fields, but it is not working 我试图为动态添加的输入字段实现keyup功能,但无法正常工作

html code HTML代码

<table  border="1" id="table">
<colgroup>
<col width="100"/>
<col width="100"/>
</colgroup>
<tr>
<td style="text-align:center;">Activity</td>
<td style="text-align:center;">Time</td>
</tr>
<tr>
<td style="text-align:center;">
<select id="activity[]">
<option value="Working">Working</option>
<option value="Leave">Leave</option>
</select>
</td>
<td style="text-align:center;"><input style="width:35px;" type="text" class="text" maxlength="3" id="day1[]"/></td>
</tr>
</table>
<br><br>
<input type="button" value="+" id="plus" />

jquery code jQuery代码

$("#plus").click(function(e){

$("#table").append("<tr><td style='text-align:center;'><select id='activity[]'><option value='Working'>Working</option><option value='Leave'>Leave</option></select></td><td style='text-align:center;'><input style='width:35px;' type='text' class='text' maxlength='3' name='day1' id='day1[]'/></td></tr>");
e.preventDefault();


});
$.each($('[id^=day1]'), function (i, item) {
         $("[id*='day1']").keyup(function () {
         if (this.value.match(/[^a-zA-Z.]/g)) {
            this.value = this.value.replace(/[^a-zA-Z.]/g, '');

        }
        });

        });

see demo here 在这里查看演示

i tried the soln here also it was also not working. 我在这里试过Soln 它也没有用。

for dynamic controls you need to bind events on document level like this: 对于动态控件,您需要像这样在文档级别绑定事件:

$(document).on("keyup","[id*='day1']",function() { 
    // code goes here...
});

or you can use body as well. 或者您也可以使用身体。

 $('body').on("keyup","[id*='day1']",function() { 
        // code goes here...
 });

Event handlers are bound only to the currently selected elements; 事件处理程序仅绑定到当前选定的元素; they must exist on the page at the time your code makes the call to .on(). 当您的代码调用.on()时,它们必须存在于页面上。 Thus in the following example, #dataTable tbody tr must exist before the code is generated. 因此,在以下示例中,在生成代码之前,#dataTable tbody tr必须存在。

If new HTML is being injected into the page, it is prefferable to use delegated events to attach an event handler, as described next. 如果要在页面中注入新的HTML,则可以使用委托事件来附加事件处理程序,如下所述。

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. 委派事件的优势在于,它们可以处理来自后代元素的事件,这些事件在以后的时间添加到文档中。 For example, if the table exists, but the rows are added dynamically using code, the following will handle it: 例如,如果该表存在,但是使用代码动态添加了行,则将通过以下方式进行处理:

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. 除了具有处理尚未创建的后代元素上的事件的能力外,委托事件的另一个优点是,当必须监视许多元素时,它们有可能大大降低开销。 On a data table with 1,000 rows in its tbody, the first code example attaches a handler to 1,000 elements. 在其正文中具有1,000行的数据表上,第一个代码示例将处理程序附加到1,000个元素。 A delegated-events approach (the second code example) attaches an event handler to only one element, the tbody, and the event only needs to bubble up one level (from the clicked tr to tbody). 委托事件方法(第二个代码示例)将事件处理程序仅附加到一个元素(即tbody),并且事件仅需要起泡一个级别(从单击的tr到tbody)。

Try this 尝试这个

$.each($('[id^=day1]'), function (i, item) {
                 $(document).on('keyup', '[id*="day1"]',function () {
                 if (this.value.match(/[^a-zA-Z.]/g)) {
                    this.value = this.value.replace(/[^a-zA-Z.]/g, '');

                }
                });

DEMO DEMO

use event delegation, also ID of an element must be unique.. so remove the id from the input field 使用事件委托,元素的ID也必须是唯一的..因此,请从输入字段中删除ID

$(document).on('keyup', 'input[name="day1"]'function () {
    if (this.value.match(/[^a-zA-Z.]/g)) {
        this.value = this.value.replace(/[^a-zA-Z.]/g, '');
    }
});

You need to add the handler inside the plus click call. 您需要在plus click调用中添加处理程序。

Your other code only gets run once. 您的其他代码仅运行一次。

Or look at live : http://api.jquery.com/live/ 或看看livehttp : //api.jquery.com/live/

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

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