Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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和多个选择值_Javascript_Select_Multidimensional Array - Fatal编程技术网

Javascript和多个选择值

Javascript和多个选择值,javascript,select,multidimensional-array,Javascript,Select,Multidimensional Array,我有一个带有2的表单,select1允许简单选择,select2允许多个选择。我想要的是,当用户在select1中选择某个内容时,select2中的关联数据应该根据我的数据数组进行选择 function selectIt() { var divArray = new Array(); // this is the value for first element on select1 divArray[0] = 3; // these are the corresponding value

我有一个带有2的表单,select1允许简单选择,select2允许多个选择。我想要的是,当用户在select1中选择某个内容时,select2中的关联数据应该根据我的数据数组进行选择

function selectIt() {
 var divArray = new Array();
 // this is the value for first element on select1
 divArray[0] = 3;
 // these are the corresponding values on select2 that should be selected
 divArray[0] = new Array();
 divArray[0][0] = 5;
 divArray[0][1] = 1;
 divArray[0][2] = 2;
 // and so on
 divArray[1] = 2;
 divArray[1] = new Array();
 divArray[1][0] = 6;
 divArray[1][1] = 3;
 divArray[1][2] = 2;

   var select2  = document.getElementById("secondSelect");
   for (var i=0; i < select2.options.length; i++)
      select2.options[i].selected = false;
}

非常感谢您的帮助

可以通过以下方式声明数组:

divArray[0] = [5, 1, 2];
我将尝试通过在没有测试的情况下编写它来回答问题,因为它可能会有一些错误:

document.getElementById("firstSelect").onchange = function() {
    var optionIndex = this.selectedIndex; // in this function 'this' is select element
    var optionsToSelect = divArray[optionIndex];
    var select2 = document.getElementById("secondSelect");
    for (var i = 0; i < optionsToSelect.length; i++){
       select2.options[optionsToSelect[i]].selected = true; 
    }
};
document.getElementById(“firstSelect”).onchange=function(){
var optionIndex=this.selectedIndex;//在这个函数中,“this”是select元素
var optionsToSelect=divArray[optionIndex];
var select2=document.getElementById(“secondSelect”);
对于(变量i=0;i
因为我需要一些数据库数据,所以我在JS中组合了一些PHP代码:

<script type="text/javascript">
function selectM() {
var divArray = new Array();

<? 
if ($result = $database->getDivs()){
    $cont = 0;
    while ($divs = mysql_fetch_array($result)) { 
        $da =  "divArray[".$divs['iddiv']."]";
        if ($resultEd = $database->getEdic($divs['iddiv'])){
            if ($cont == 0) $da .= " = [";
            $tot    = mysql_num_rows($resultEd);
            while ($divEd = mysql_fetch_array($resultEd)) { 
                $da .= $divEd['ed_id']." ";
                $cont++;
                if ($cont < $tot) $da .= ", ";

            }
        }
        $da .= "]; ";
        $cont = 0;
        echo $da;
    }
}  
?>

var largo, valor;
var inexE           = document.getElementById("idDiv").value ;
var ediciones       = document.getElementById("ediciones");
  if (inexE > 0) {
    var toSelect = divArray[inexE];
    for (var i=0; i < ediciones.options.length; i++) {
        ediciones.options[i].selected = false;
        valor       = ediciones.options[i].value;
        largo       = toSelect.length;
        for(var j = 0; j < largo; j++) {
            if(toSelect[j] == valor) 
                ediciones.options[i].selected = true;
        }
    }
  } else {
    for (var i=0; i < ediciones.options.length; i++) 
        ediciones.options[i].selected = false;
  }

}
</script>

函数selectM(){
var divArray=新数组();
瓦拉戈,英勇;
var inxe=document.getElementById(“idDiv”).value;
var-ediciones=document.getElementById(“ediciones”);
如果(不精确>0){
var-toSelect=divArray[inxe];
对于(变量i=0;i
工作完美


谢谢你为我指明了正确的方向

你不喜欢jquery库?在这里使用函数和循环会容易得多。我不知道如何使用jquery,我是js新手。如果您能构建一些php对象,用您的数据填充它,然后使用:var divArray会更好=
document.getElementById("firstSelect").onchange = function() {
    var optionIndex = this.selectedIndex; // in this function 'this' is select element
    var optionsToSelect = divArray[optionIndex];
    var select2 = document.getElementById("secondSelect");
    for (var i = 0; i < optionsToSelect.length; i++){
       select2.options[optionsToSelect[i]].selected = true; 
    }
};
<script type="text/javascript">
function selectM() {
var divArray = new Array();

<? 
if ($result = $database->getDivs()){
    $cont = 0;
    while ($divs = mysql_fetch_array($result)) { 
        $da =  "divArray[".$divs['iddiv']."]";
        if ($resultEd = $database->getEdic($divs['iddiv'])){
            if ($cont == 0) $da .= " = [";
            $tot    = mysql_num_rows($resultEd);
            while ($divEd = mysql_fetch_array($resultEd)) { 
                $da .= $divEd['ed_id']." ";
                $cont++;
                if ($cont < $tot) $da .= ", ";

            }
        }
        $da .= "]; ";
        $cont = 0;
        echo $da;
    }
}  
?>

var largo, valor;
var inexE           = document.getElementById("idDiv").value ;
var ediciones       = document.getElementById("ediciones");
  if (inexE > 0) {
    var toSelect = divArray[inexE];
    for (var i=0; i < ediciones.options.length; i++) {
        ediciones.options[i].selected = false;
        valor       = ediciones.options[i].value;
        largo       = toSelect.length;
        for(var j = 0; j < largo; j++) {
            if(toSelect[j] == valor) 
                ediciones.options[i].selected = true;
        }
    }
  } else {
    for (var i=0; i < ediciones.options.length; i++) 
        ediciones.options[i].selected = false;
  }

}
</script>