gpt4 book ai didi

javascript - 从 javascript 设置按钮 id

转载 作者:行者123 更新时间:2023-11-28 17:11:06 25 4
gpt4 key购买 nike

我有一个下拉列表,其中填充了从数据库获取的数据。我需要将此下拉列表的选定索引值指定为按钮的 ID。真的可以吗?

我的代码

<select name="customer" onchange="getCustomer()"  id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
<?php
$sql = mysqli_query($db,"SELECT * FROM customer ");
while($row= mysqli_fetch_array($sql))
{
?>
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
<?php
}
?>

</select>
</div>
<button name="custbt" id="1" class="label-text col-form-label userinfo"><a href="#vCenteredModal" data-toggle="modal">Show customer details</a></button>

<script>
function getCustomer()
{
var e = document.getElementById("customer");
var cust = e.options[e.selectedIndex].value;
// alert(cust);custbt
//document.getElementByName("custbt").id=cust;
document.getElementByName(custbt).id=cust;

}
</script>

最佳答案

也许这会解决您的问题。我在 btn 上添加了一个名为 CustomerModalBttn 的类。

试试这个

<select name="customer" id="customer" class="form-control" style="width: 180px !IMPORTANT;">
<option value="">--Select Customer--</option>
<?php
$sql = mysqli_query($db,"SELECT * FROM customer ");
while($row= mysqli_fetch_array($sql)){
?>
<option value="<?php echo $row['custid'];?>"><?php echo $row['customername'];?></option>
<?php
}
?>
</select>

<button name="custbt" id="1" class="label-text col-form-label userinfo CustomerModalBttn">
<a href="#vCenteredModal" data-toggle="modal">Show customer details</a>
</button>

<script>
$(document).ready(function(){
$('#customer').on('change',function(){
$('.CustomerModalBttn').attr('id',$(this).val());
});
});
</script>

关于javascript - 从 javascript 设置按钮 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54400566/

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