我有一张桌子,上面有一个“添加行”按钮。此按钮使用JQuery动态添加一行。它通过复制第一个...,然后将所有id =“ ..”替换为递增的数字来工作。
问题是这些行具有一个YUI自动完成功能,如下所示:
<td>
<input type="hidden" name="location_num[0]" value="508318" maxLength="25" style="width:230px" id="location_num[0]"/>
<input type="textbox" name="location_numDisplayDesc[0]" value="WINNIPEG" maxLength="25" style="width:230px" id="location_numDisplayDesc[0]"/>
<div id="Container_location_num[0]" style="display:inline;"></div>
<script type="text/javascript">
// Initialize autocomplete
var location_numAC = new YAHOO.widget.AutoComplete(
"location_numDisplayDesc[0]",
"Container_location_num[0]",
locationDataSource,
acConfig);
location_numAC.useShadow = true
location_numAC.useIFrame = true
location_numAC.dataErrorEvent.subscribe(acErrorFunction);
// Format results to include the reference number
location_numAC.formatResult = function(resultItem, query) {
return resultItem[0];
};
// Clear key before request
location_numAC.dataRequestEvent.subscribe(function fnCallback(e, args) {
YAHOO.util.Dom.get("location_num[0]").value = ""; });
// Set key on item select
location_numAC.itemSelectEvent.subscribe(function(event, args) {
YAHOO.util.Dom.get("location_num[0]").value = args[2][1];
});
// Clear key when description is cleared
location_numAC.textboxBlurEvent.subscribe(function fnCallback(e, args) {
if (isEmpty(YAHOO.util.Dom.get("location_numDisplayDesc[0]").value)) {
YAHOO.util.Dom.get("location_num[0]").value = "";
} // end if
});
</script>
</td>
该代码在Firefox中可以正常工作,并且新创建的自动完成功能可以正常工作,但是在IE(6&7)中,我收到一条错误消息,这意味着未成功创建location_num_AC。我认为这是因为它没有按原样读取新创建的输入或div。我试过用
$("Container_location_num[0]").ready(function {...});
但这似乎不起作用。还有其他想法吗?
最佳答案
IE中插入到DOM中的表单域不会像您期望的那样添加到表单集合中。
通常,您可以使用以下两种方式之一来引用表单字段:
document.forms[0]["myFormName"];
document.forms[0][12];
也就是说,通过其表单字段名称或索引。但是,当您在IE中将表单字段添加到DOM时,您不能仅通过其索引来按名称引用它。如果您的代码(或任何支持代码)正在按名称在集合中查找表单字段,那么您显然会遇到问题。
如果唯一的键是名称,则可以按索引遍历所有表单字段并找到要查找的内容,但这显然是线性操作。您还可以遍历并查找哪些表单字段被数字索引,而不是按名称索引,并自己更新表单对象。
我没有足够的细节来知道这在您的项目中是如何发生的(或是否发生),但这是IE怪癖之一,因为您正在动态添加字段,所以听起来它可能正在发挥作用。
关于javascript - 动态添加的JavaScript在IE中找不到动态添加的字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1703909/