Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.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 在JS中动态创建函数_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在JS中动态创建函数

Javascript 在JS中动态创建函数,javascript,jquery,html,Javascript,Jquery,Html,所以我找到了这个,我想把它和。因此,当您单击按钮3次时,它们的id分别为0、1和2。然后,我希望能够对其他选择元素隐藏选定选项的值。我试图根据工作示例动态创建函数,但没有成功。任何帮助都将不胜感激 第一小提琴 JavaScript $("#first").change(function() { if($("#first").val() != "none") { $("#second option[value!="+$("#first").val()+"]").show()

所以我找到了这个,我想把它和。因此,当您单击按钮3次时,它们的id分别为0、1和2。然后,我希望能够对其他选择元素隐藏选定选项的值。我试图根据工作示例动态创建函数,但没有成功。任何帮助都将不胜感激

第一小提琴

JavaScript

$("#first").change(function() {
    if($("#first").val() != "none") {
        $("#second option[value!="+$("#first").val()+"]").show();
        $("#second option[value="+$("#first").val()+"]").hide();
    } else {
        $("#second option[value!="+$("#first").val()+"]").show();
    }
});

$("#second").change(function() {
    if($("#second").val() != "none") {
        $("#first option[value!="+$("#second").val()+"]").show();
        $("#first option[value="+$("#second").val()+"]").hide();
    } else {
        $("#first option[value!="+$("#second").val()+"]").show();
}
});
arrSelects = [];

function generateSelect() {
    var para = document.createElement("select");
    $(para).attr('class', 'selectClass');
    $(para).attr('id', arrSelects.length);
    document.getElementById("createHere").appendChild(para);
    arrSelects.push(' ');
    fillSelect();
}

function fillSelect() {
    $( ".selectClass").each( function () {
        if ($(this).has('option').length < 1) {
            for (var i = 0; i < 5; i++){
                $(this).append('<option value='+i+'>'+i+'</option> ');
            }
        }
    })
}
HTML


没有一个
丰田
日产
闪避
没有一个
丰田
日产
闪避
副手

JavaScript

$("#first").change(function() {
    if($("#first").val() != "none") {
        $("#second option[value!="+$("#first").val()+"]").show();
        $("#second option[value="+$("#first").val()+"]").hide();
    } else {
        $("#second option[value!="+$("#first").val()+"]").show();
    }
});

$("#second").change(function() {
    if($("#second").val() != "none") {
        $("#first option[value!="+$("#second").val()+"]").show();
        $("#first option[value="+$("#second").val()+"]").hide();
    } else {
        $("#first option[value!="+$("#second").val()+"]").show();
}
});
arrSelects = [];

function generateSelect() {
    var para = document.createElement("select");
    $(para).attr('class', 'selectClass');
    $(para).attr('id', arrSelects.length);
    document.getElementById("createHere").appendChild(para);
    arrSelects.push(' ');
    fillSelect();
}

function fillSelect() {
    $( ".selectClass").each( function () {
        if ($(this).has('option').length < 1) {
            for (var i = 0; i < 5; i++){
                $(this).append('<option value='+i+'>'+i+'</option> ');
            }
        }
    })
}
arrSelects=[];
函数generateSelect(){
var para=document.createElement(“选择”);
$(para.attr('class','selectClass');
$(para.attr('id',arr.length));
document.getElementById(“createHere”).appendChild(第段);
arr.push(“”);
fillSelect();
}
函数fillSelect(){
$(“.selectClass”)。每个(函数(){
if($(this).has('option')。长度<1){
对于(变量i=0;i<5;i++){
$(this.append(“”+i+“”);
}
}
})
}
HTML

点击我!

two
onclick
。也许,
onclick=“generateSelect();generateSelect()”
Ah,是的,我相信这是真的,但我仍然无法动态创建函数(将代码合并在一起)