简体   繁体   English

jQuery自动完成UI窗口小部件-在动态创建的表行元素上执行jQuery select事件

[英]jQuery autocomplete UI Widget- Perform jQuery select event on dynamically created table row element

I have a working jQuery autocomplete being performed on the text input of a table data element, txtRow1. 我在表数据元素txtRow1的文本输入上执行了有效的jQuery自动完成功能。 The text data is remote, from a mysql database, and is returned by JSON as 'value' for the text input. 文本数据来自mysql数据库,是远程的,并且由JSON作为文本输入的“值”返回。 The returned data includes another piece of text, which, via a select event within the autocomplete, is populated to the adjacent table data element tickerRow1. 返回的数据包括另一段文本,该文本通过自动完成中的select事件填充到相邻的表数据元素tickerRow1中。

With help from the SO community, the autocomplete is now live and working on all text input elements of a dynamically created table (so txtRow1 to txtRowN). 在SO社区的帮助下,自动完成功能现已启用,并且可以在动态创建的表(从txtRow1到txtRowN)的所有文本输入元素上使用。 There is javascript code to create and name the table elements txtRoxN + 1 and tickerRowN + 1. 有JavaScript代码可以创建和命名表元素txtRoxN +1和tickerRowN +1。

However, I have a problem with the select event for the id of tickerRowN. 但是,我对tickerRowN的id的select事件有疑问。 Because it changes every time I add a row, I don't know how to call the select event for the specific id of the table data in question. 因为每次添加一行时它都会更改,所以我不知道如何为有问题的表数据的特定ID调用select事件。

I have done a lot of searching around but as I am new to this, the only functions I have been able to find manipulate the element data when you know the id already. 我已经做了大量的搜索工作,但是由于这是我的新手,当您已经知道id时,我能够找到的唯一函数就可以操作元素数据。 This id is dynamically created and so I don't know how to build the syntax. 此ID是动态创建的,因此我不知道如何构建语法。

Thankyou for your time. 感谢您的时间。

UPDATE: with huge thanks to JK, the following example works. 更新:非常感谢JK,以下示例适用。 I now know about jsFiddle and will try to use this for all further questions. 我现在了解jsFiddle,并将尝试将其用于所有其他问题。 The following code works for my dynamic example, but I don't know why. 以下代码适用于我的动态示例,但我不知道为什么。 Sigh. 叹。

jsFiddle working example jsFiddle工作示例

function getRowId(acInput){
     //set prefix, get row number
     var rowPrefix = 'txtRow';
     var rowNum = acInput.attr("id").substring((rowPrefix.length));
     return rowNum;
}

$("#txtRow1").autocomplete({
    source: states,
    minLength: 2,
    select: function(event, ui) {
       var tickerRow = "#tickerRow" + getRowId($(this));
        //set ticker input
        $(tickerRow).val(ui.item.label);
    }
});

http://jsfiddle.net/jensbits/BjqNz/ http://jsfiddle.net/jensbits/BjqNz/

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

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