Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有选定值的javascript foreach选择循环_Javascript_Php_Jquery - Fatal编程技术网

具有选定值的javascript foreach选择循环

具有选定值的javascript foreach选择循环,javascript,php,jquery,Javascript,Php,Jquery,我编写了以下代码,生成一个选择列表。但是,应该检查this.id是否与现有_代码相同并将所选值放入其中的部分似乎不起作用 你知道怎么了吗 function showHide(layer, existing_code) { jQuery.ajax({ url: 'getdata.php', dataType: 'json', cache: 'false', success: function(data) { var codes = codes; var selectcode =

我编写了以下代码,生成一个选择列表。但是,应该检查this.id是否与现有_代码相同并将所选值放入其中的部分似乎不起作用

你知道怎么了吗

function showHide(layer, existing_code) {

jQuery.ajax({ url: 'getdata.php', dataType: 'json', cache: 'false', success: function(data) {

    var codes = codes;
    var selectcode = '';

    selectcode += '<select name="codes">';  
        jQuery.each(codes, function() {
            selectcode += '<option value="' + this.id + '"';
                if (this.id == existing_code) {
                    selectcode += ' selected';
                }
            selectcode += '>' + this.code + '</option>';
        });
    selectcode += '</select>';  

    jQuery('.'+layer).html(selectcode);

}});

}
函数showHide(层,现有的\u代码){
ajax({url:'getdata.php',数据类型:'json',缓存:'false',成功:函数(数据){
var代码=代码;
var selectcode='';
选择code+='';
每个(代码,函数(){
选择code+=''+this.code+'';
});
选择code+='';
jQuery('.'+layer).html(选择代码);
}});
}

这可能就是您想要的:

function showHide(layer, existing_code) {

    jQuery.ajax({ url: 'getdata.php', dataType: 'json', cache: 'false', success: function(data) {

        var codes = data;
        var selectcode = '';

        selectcode += '<select name="codes">';
        jQuery.each(codes, function(i, code) {
            selectcode += '<option value="' + code.id + '"';
            if (code.id === existing_code) {
                selectcode += ' selected';
            }
            selectcode += '>' + code.code + '</option>';
        });
        selectcode += '</select>';

        jQuery('.'+layer).html(selectcode);

    }});
函数showHide(层,现有的\u代码){
ajax({url:'getdata.php',数据类型:'json',缓存:'false',成功:函数(数据){
var代码=数据;
var selectcode='';
选择code+='';
每个(代码,函数(i,代码){
选择code+=''+code.code+'';
});
选择code+='';
jQuery('.'+layer).html(选择代码);
}});

}

var代码=代码-嗯,这不是很有用,是不是。。。您在
选择code
中也没有“”。。。因此,您不会使用此代码创建
元素-从ajax返回的
数据也会被忽略,因此,不确定您期望的是什么