这是我的代码片段,我动态创建输入框:
if (data != null) {
locations = data;
for (var i = 0; i < locations.length; i++) {
var newLineItem = domEditor.CreateElement({ type: "li" });
var roomsLink = domEditor.CreateElement({ id: locations[i].Id, name: "locationRadioBtns", type: "a", href: "#", onclick: self.SelectLocation, innerHtml: locations[i].DisplayName, checked: i == 0, parent: $("#LocationRadioButtons") });
$(newLineItem).append(roomsLink);
$(LocationRadioButtons).append(newLineItem);
}
self.Display();
}
显示方法,我需要获取a href标签
创建的第一个元素的idthis.Display = function () {
console.log($('#locationRadioBtns').val(0)); //value of your default option
};
它以未定义的方式返回
答案 0 :(得分:1)
您想要循环创建的第一个元素的id
- 属性吗?
尝试
console.log($('#locationRadioBtns a').first().attr('id'));