gpt4 book ai didi

javascript - 获取选中下拉列表的数据

转载 作者:行者123 更新时间:2023-11-30 20:59:55 25 4
gpt4 key购买 nike

当我在下拉列表中选择或选择产品时,如何获取 product_rate 的值并将其放入禁用的输入字段中。

enter image description here

enter image description here

<?php
$con = mysqli_connect("localhost", "root", "", "dbselectedropdown");

if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<title>Selected Dropdown</title>
</head>
<body>
<select name="product_select" id="product_select">
<option>--SELECT--</option>
<?php
$query = mysqli_query($con, "SELECT * FROM tbselectedropdown");
while ($row = mysqli_fetch_array($query)) {
echo "<option>" .$row["product_name"]. "</option>";
}
?>
</select><br/><br/><br/><br/><br/><br/>
<input type="text" name="product_inputfield" id="product_inputfield" disabled/>
</body>
<script>

</script>
</html>

最佳答案

首先,改进PHP:

$query = mysqli_query($con, "SELECT * FROM tbselectedropdown");
while ($row = mysqli_fetch_array($query)) {
echo "<option value=" . $row["product_rate"] . ">" .$row["product_name"]. "</option>";
}

二、添加脚本:

<script>
var input = document.getElementById('product_select')
input.addEventListener('change', function (e) {
document.getElementById('product_inputfield').value = e.value
})
</script>

或者,如果您使用的是 jQuery :)

<script>
$('#product_select').change(function () {
$('product_inputfield').val($(this).val())
})
</script>

关于javascript - 获取选中下拉列表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47239432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com