[英]How to fetch the id of dynamically generated textboxes?
How is it possible to get the id of the dynamically generated textboxes using jquery?. 如何使用jquery获取动态生成的文本框的ID? I need to fire the TextChanged event for the corresponging textbox.
我需要为相应的文本框触发TextChanged事件。 There is no method reference for the textboxes in the code behind.How can i refer to any method in the codebehind on firing the event.
后面的代码中没有文本框的方法引用。触发事件后,如何在代码中引用任何方法。 Somebody please help.
有人请帮忙。 I dont know jquery much.
我不太了解jquery。 The entire script im using is as as follows:
我使用的整个脚本如下:
<script type="text/javascript">
$(init);
function init()
{
$('#test').droppable(// Div Control
{
drop: handleDropEvent
});
$('a').each(function(idx, item) {
$(item).draggable({ cursor: 'move', helper: 'clone' })
});
}
$(function() {
$("#draggable").draggable(); //Nothing to do with this div
});
function handleDropEvent(event, ui)
{
var draggable = ui.draggable;
document.getElementById('test').innerHTML += addColumn(draggable.attr('text')) + '<br>';
}
function addColumn(column)
{
var iHtml;
// This code will generate a checkbox and a textbox. I need to fire the event of thus generated textboxes.
iHtml = '<div id="dv' + column + '" width="100px;" height="20px;" padding: "0.5em;"> ' + '<span title="ToolTipText">' + '<input type="checkbox" id="cb' + column + '" value="' + column + '" /> <label for="cb' + column + '">' + column + '</label></span><input type="text" runat="server" id="aln' + column + '"> </div>';
return iHtml;
}
</script>
There's two ways: keep the generated element, or generate an ID when you generate your new element. 有两种方法:保留生成的元素,或在生成新元素时生成ID。
a) keep the generated element a)保留生成的元素
This requires that you don't use innerHTML, but create the element (with document.createElement
, or with jQuery's $
), and then you can use the element directly (no need to call it by ID any more). 这要求您不使用innerHTML,而是创建元素(使用
document.createElement
或jQuery的$
),然后可以直接使用元素(不再需要通过ID调用它)。 For instance, with jQuery: 例如,使用jQuery:
var container = $('#container');
var myDiv = $('<div id="myDiv"/>');
var myCheck = $('<input type="checkbox"/>');
myDiv.append(myCheck);
container.append(myDiv);
b) generate the ID b)生成ID
container.innerHTML = '<div id="myDiv"><input type="checkbox" id="myCheck"/></div>';
// and after this you can get them by ID:
var myCheck = $('#myCheck');
I would just add a class to the textbox in your iHtml then use .live() event 我只是将一个类添加到您的iHtml中的文本框中,然后使用.live()事件
replace your iHtml with this 以此替换您的iHtml
iHtml = '<div id="dv' + column + '" width="100px;" height="20px;" padding: "0.5em;"> ' + '<span title="ToolTipText">' + '<input type="checkbox" id="cb' + column + '" value="' + column + '" /> <label for="cb' + column + '">' + column + '</label></span><input class="myclass" type="text" runat="server" id="aln' + column + '"> </div>';
then add the live event 然后添加直播活动
$('.myclass').live('change', function() {
alert(' Live handler called.');
});
here is a WORKING DEMO 这是一个工作演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.