如何从 href 属性获取默认 id 值

How to get the default id value from an href attribute

本文关键字:默认 id 获取 属性 href      更新时间:2023-09-26

这是我动态创建输入框的代码片段:

 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();
    }

显示方法,我需要获取 href 标签创建的第一个元素的 id

this.Display = function () {
    console.log($('#locationRadioBtns').val(0)); //value of your default option
};

它以未定义返回

你想要循环创建的第一个元素的id -属性吗?

尝试

console.log($('#locationRadioBtns a').first().attr('id'));