动态填充动态创建的选择列表 [英] Dynamically populate dynamically created select list

查看:83
本文介绍了动态填充动态创建的选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点困难,在这里;我使用JS动态创建选择框,但我需要Ajax来填补他们的选择。到目前为止,我的code为返回undefined了,我真不知道该怎么办这里。我的PHP返回的信息很好,但JS是不是解析它。另一双眼睛,或其他脑部充分了解将AP preciated这里;

I'm having a bit of difficulty here; I am using js to dynamically create select boxes, but I need Ajax to fill them with options. So far, my code is returning undefined, and I frankly don't know what to do here. My php returns the info fine, but the js isn't parsing it. Another set of eyes, or another brain full of knowledge would be appreciated here;

function getSkilllist(group) {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","skl_lst_gen2.php?group=" + group + "&t=" + Math.random(),true);
    xmlhttp.send();
}

function addInput(divName,group) {
var skillst = getSkilllist(group);
var newdiv = document.createElement('div');
newdiv.innerHTML = '<select name="ski[]">' + skillst + '</select> .....

该功能的其余部分是好的,但无功skillst被返回undefined如说,我想不通这是为什么。我认为它是与字符串,但我想不出有什么需要做的事情。

The rest of the function is fine, but the var skillst is returning undefined as stated, and I can't figure out why. I assume it has something to do with strings, but I can't figure out what needs to be done.

推荐答案

您的函数不返回任何东西,这就是为什么它不工作。试试这个:

Your function does not return anything, which is why it isn't working. Try this:

function getSkilllist(group) {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
       var newdiv = document.createElement('div');
       newdiv.innerHTML = '<select name="ski[]">' + xmlhttp.responseText + '</select> .....

       //place in DOM here
    }
}
xmlhttp.open("GET","skl_lst_gen2.php?group=" + group + "&t=" + Math.random(),true);
xmlhttp.send();
}

function addInput(divName,group) {
getSkilllist(group);

这篇关于动态填充动态创建的选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆