简体   繁体   English

在下拉列表中获取选定的值并将其放入其他下拉列表中?

[英]Get selected value in dropdown list and put it inside other dropdown list?

This is my script : 这是我的脚本:

function myFunction() {
    var x = document.getElementById("mySelect").value;
    var xtest = x.split("|");
    x = xtest[1];
    document.getElementById("demo").innerHTML = x;
    var text = $('#demo').html();
    $('#texted').val(text);
}

$(document).ready(function() {
    myFunction();
    $("#mySelect").change(function() {
        myFunction();
        sum();
    });
    sum();
    $("#num1, #texted").on("keydown keyup", function() {
        sum();
    });
    $("#num1").mouseup(function() {
        sum();
    });
});

function sum() {
    var num1;
    num1 = document.getElementById('num1').value;
    document.getElementById('sum').value = num1;
    var texted = document.getElementById('texted').value;
}

this is from where i'm getting the value type (text) 这是我从中获取值类型(文本)的地方

echo"<select name='id_type' id='mySelect' onchange='myFunction()' class='form-control select' data-live-search='true'>";
foreach($data as $r){
echo"<option value=".$r['id_type']."|".$r['libelle'].">".$r['libelle']."</option>";
}
echo"</select>";

<p id="demo"></p>

this is where i want to put the value that i have selected 这是我要放置我选择的值的地方

echo"<select name='echange' id='texted' class='form-control select' data-live-search='true'>";
foreach($data as $r){
echo"<option value=".$r['id_annee'].">".$r['libelle']."</option>";
}
echo"</select>";

PS the purpose of this method is to make it easy to find specific data in the last select PS此方法的目的是使在最后选择中轻松查找特定数据成为可能

附加屏幕截图

It's fine to build each list using PHP, but you'll definitely want to use JavaScript to manipulate the values of each list. 使用PHP构建每个列表是可以的,但是您肯定要使用JavaScript来操纵每个列表的值。 For example (I'm going to use straight JS here) you could do something like this 例如(我将在这里使用纯JS),您可以执行以下操作

list1 = document.getElementById("mySelect");
list2 = document.getElementById("texted");

list1.onblur = function() {
    list2.value = list1.options[list1.selectedIndex].value;
    //add option
    var opt = document.createElement('option');
    opt.value = list1.options[list1.selectedIndex].value;
    opt.innerHTML = list1.options[list1.selectedIndex].value;
    list2.appendChild(opt);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM