从JavaScript对象访问选择的HTML元素

时间:2014-06-01 23:40:04

标签: javascript jquery

我有JavaScript object返回HTML information来填充响应式Web应用程序。我在访问带有类的选择框时遇到问题,以识别它并获取它包含的选定选项。

以下是我正在做的事情:

var EditRowRestriction = OptionRules.prototype.getEditRowRestriction(j); <!-- THIS IS THE OBJECT THAT CALLS A METHOD TO POPULATE HTML ELEMENTS TO BE SENT BACK TO THAN BE USED TO POPULATE SCREEN
var check = $(EditRowRestriction).find("select");

console.log(check);

以下是check将回复的示例:

select.restirction-select <!-- THIS IS WHAT I WANT
context: tr
length: 1
prevObject: v.fn.v.init[1]
selector: "select"
__proto__: Object[0]

现在我能够记录下来,如果我尝试做类似的事情,check[0].find(':selected').text();

它会回复错误:Uncaught TypeError: undefined is not a function

现在如果我输出check[0],我就会得到:<select></select>

有什么建议吗?

更新:

以下是如何填充select的示例

var sel = document.createElement("select");

    $.ajax({
        url: "actions/....php",
        type: "post",
        dataType: "json",
        data:
        {
            'function' : "admin_populate_global",
            'elevation_name' : '',
            'user' : $("li.selected").text()
        },
        success: function(data){

            // FIRST MAKE SURE DATA WAS FOUND
            if(data["status"] == "found")
            {
                    var restrictionArray = data["data_retrieved"].split(',');
                        var events = $(".option-rule-item").map(function() {
                            if($(this).hasClass("selected"))
                            {
                            }
                            else
                                return this.innerHTML;
                        }).get();

                        var checkReturnedText = $.trim(restrictionArray[loop_amount]);

                        sel.className ="restirction-select";

                        var opt = document.createElement("option");

                        opt.innerHTML = checkReturnedText;
                        opt.setAttribute("value", checkReturnedText);
                        sel.appendChild(opt);
                        for(var i = 0; i < events.length; i++)
                        {
                            if(checkReturnedText == events[i])
                            {
                            }
                            else
                            {
                                var opt = document.createElement("option");
                                var event = events[i];
                                opt.innerHTML = event;
                                opt.setAttribute("value", event);
                                sel.appendChild(opt);
                            }
                        }


            }
        },
        error:function(){
            alert("An error occured, please try again.");
        }
    });
    return sel;

0 个答案:

没有答案