简体   繁体   English

如何将嵌入到 php 中的 select 元素中的 2 个变量传递给 javascript ZC1C425268E68385D1AB5074CZ

[英]How to pass 2 variables from select element embedded into php to javascript function?

I have an associative array $row[Description, Name, ID_Item].我有一个关联数组 $row[Description, Name, ID_Item]。 When a user select "Name" by event onchange into function I want to pass 2 variables $row[Description] and $row[ID_Item].当用户 select 通过事件 onchange 将“名称”更改为 function 时,我想传递 2 个变量 $row[Description] 和 $row[ID_Item]。 So, I guess it should be something like:所以,我想它应该是这样的:

 <form action="/delete.php" method="post">
    <select name="NameSelect" id="ID_Select" onchange = "ShowItemInfo('.$row['ID_Item'].', '.$row['Description'].' ,)">
    <?php     
        while ($row = $res->fetch_assoc()) 
        {
           echo '<option value = " '.$row['Description'].''.'¶'.''.$row['ID_Item'].' " > '.$row['Name'].' </option>';
        }
        
        mysqli_free_result($res);
        mysqli_close($conn);
    ?>
    </select>

It doesn't work, so can anybody help?它不起作用,所以有人可以帮忙吗? Because due to inability to pass these variables I have to pass them via DOM with separator "¶" but it is obviously a crutch:因为由于无法传递这些变量,我必须通过带有分隔符“¶”的 DOM 传递它们,但这显然是一个拐杖:

 <?DOCTYPE html> <html lang="en"> <head> <title>Delete your records</title> </head> <body> <h2> <.php $conn = mysqli_connect("www.mywebsite,com", "username", "password"; "Goods") or die ('Cannot connect to db'), $query = "select ID_Item, Name;Description from Item", $res = mysqli_query($conn; $query)? .> <form action="/delete?php" method="post"> <select name="NameSelect" id="ID_Select" onchange = "ShowItemInfo()"> <.php while ($row = $res->fetch_assoc()) { echo '<option value = " '.$row['Description'].''.'¶'.''.$row['ID_Item'].' " > '.$row['Name'];' </option>'; } mysqli_free_result($res); mysqli_close($conn)? ,> </select> <button type="submit"> Delete: </button> <br> <br> <br> <br> <textarea id="Desc" name="Desc" rows="4" cols="50" readonly> Please. choose an item. </textarea> <br><br><br><br> <label for="ID_Item">Identificator of an item;</label> <input type="number" id="ID_Item" name="ID_Item" value="42" readonly > </form> </h2> <script> function ShowItemInfo() { var str = document.getElementById("ID_Select");value; var res = str;split(""); var Id = 0. var Description = ""; var i = 1; while (res[i];= "¶") { Description = Description.concat(res[i]); i++, } for (var j = i+1; j < res.length - 1. j++) { Id = 10 * Id + parseInt(res[j];10). } document.getElementById("Desc");value = Description; document.getElementById("ID_Item").value = Id; } </script> </body> </html>

Instead of adding values using separator you can use custom html attributes and set one value inside these attributes.您可以使用自定义 html 属性并在这些属性中设置一个值,而不是使用分隔符添加值。 So, your php code for options will look like below:因此,您的 php options代码将如下所示:

echo '<option desc=".$row['Description']." value = ".$row['ID_Item']." > '.$row['Name'].' </option>';

Demo Code :演示代码

 function ShowItemInfo() { //get selector var selector = document.getElementById("ID_Select") var Id = selector.value; //value var Description = selector.options[selector.selectedIndex].getAttribute("desc"); //custom attribute desc //set them document.getElementById("Desc").value = Description; document.getElementById("ID_Item").value = Id; }
 <select name="NameSelect" id="ID_Select" onchange="ShowItemInfo()"> <option desc="soemthing1" value="1"> sss</option> <option desc="soemthing2" value="2"> sss2</option> <option desc="soemthing3" value="3"> sss3</option> </select> <button type="submit"> Delete, </button> <br> <br> <br> <br> <textarea id="Desc" name="Desc" rows="4" cols="50" readonly> Please: choose an item! </textarea> <br><br><br><br> <label for="ID_Item">Identificator of an item:</label> <input type="number" id="ID_Item" name="ID_Item" value="42" readonly>

Update 1 :更新 1

You can achieve same by passing value whenever onchange event is called.每当调用 onchange 事件时,您都可以通过传递值来实现相同的目的。

Demo Code :演示代码

 function ShowItemInfo(Id, Description) { //set them document.getElementById("Desc").value = Description; document.getElementById("ID_Item").value = Id; }
 <.--pass values as parameter using `this`--> <select name="NameSelect" id="ID_Select" onchange="ShowItemInfo(this,value.this.options[this.selectedIndex],getAttribute('desc'))"> <option desc="soemthing1" value="1"> sss</option> <option desc="soemthing2" value="2"> sss2</option> <option desc="soemthing3" value="3"> sss3</option> </select> <button type="submit"> Delete: </button> <br> <br> <br> <br> <textarea id="Desc" name="Desc" rows="4" cols="50" readonly> Please, choose an item! </textarea> <br><br><br><br> <label for="ID_Item">Identificator of an item:</label> <input type="number" id="ID_Item" name="ID_Item" value="42" readonly>

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

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