Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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设置按钮id_Javascript_Php_Css - Fatal编程技术网

从javascript设置按钮id

从javascript设置按钮id,javascript,php,css,Javascript,Php,Css,我有一个下拉列表,里面充满了从数据库中获取的数据。我需要将此下拉列表的选定索引值指定为按钮的ID。 真的有可能吗 我的代码 <select name="customer" onchange="getCustomer()" id="customer" class="form-control" style="width: 180px !IMPORTANT;"> <option value="">--Select Customer--</option>

我有一个下拉列表,里面充满了从数据库中获取的数据。我需要将此下拉列表的选定索引值指定为按钮的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>

ID必须以字母开头

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

var customer = document.getElementById("customer");
    var a1 = document.getElementById("a1");

    customer.addEventListener("change",function(){
        a1.removeAttribute("id");
        a1.setAttribute("id",customer.value);
    });

ID必须以字母开头

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

var customer = document.getElementById("customer");
    var a1 = document.getElementById("a1");

    customer.addEventListener("change",function(){
        a1.removeAttribute("id");
        a1.setAttribute("id",customer.value);
    });

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

试试这个

<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>

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

试试这个

<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>
只要在$row['custid']=$\u GET['custid']echo“选中”的情况下添加一些条件,如果您从url传递客户id,然后使用$\u GET参数获取它。

只要在$row['custid']=$\u GET['custid']echo“选中”的情况下添加一些条件,如果您从url传递客户id,然后使用$\u GET参数获取它。


有可能,什么对您不起作用?您应该设置数据id而不是id。custbt未定义,因此document.getElementByNamecustbt.id=cust;不起作用。有可能,什么对您不起作用?您应该设置数据id而不是id。custbt未定义,因此document.getElementByNamecustbt.id=cust;不起作用。也许会改变,也许会改变