doucai6663 2019-05-29 19:02
浏览 55
已采纳

从填充列表中获取选定的选项ID

I have a radiobutton list that is populated from MySQL. A function to be called when the selected radiobutton changes. I would like to be get the ID of the selected item.

 <ul id="radio" class="input-list">
<?php
$stmt = mysqli_prepare($link, "SELECT id, name, price FROM cases ORDER BY price");
$stmt->execute();
$stmt->bind_result($case_id, $case_name, $case_price);
while($stmt->fetch()) {
echo '<li>
<input class="selectedoptions" id="'.$case_id.'" name="config-case" value="'.$case_price.'" type="radio" onchange="updateImage(this.id);">
<label class="sub-label" for="'.$case_id.'">'.$case_name.'         [£'.$case_price.']</label>
</li>';
}
$stmt->close();
>
</ul>

JS:

<script>

    function updateImage(caseid) {

        var selectmenuID = document.getElementById(caseid);
        console.log(selectmenuID);
    }

<script>

Attempting to get the id of the selected item is not successful. This is the console output:

<input class="selectedoptions" id="32" name="config-case" value="24"
type="radio" onchange="updateImage(this.id);">

I would like to get the 32 (the id). How can I do this? Thank you,

  • 写回答

2条回答 默认 最新

  • drn34916 2019-05-29 19:08
    关注

    Your code is right. The only fact about it is that you are logging in console the DOM element, not the parameter you recive caseid which value is the ID attribute of the element.

    Look at an example:

    function updateImage(caseid) {
      var selectmenuID = document.getElementById(caseid);
      console.log(`Clicked on radio input with id:${caseid}`);
    }
    <ul id="radio" class="input-list">
    
      <li>
        <input class="selectedoptions" id="1" name="config-case" value="'.$case_price.'" type="radio" onchange="updateImage(this.id);">
        <label class="sub-label" for="'.$case_id.'">Element with ID 1</label>
      </li>
      
      <li>
        <input class="selectedoptions" id="2" name="config-case" value="'.$case_price.'" type="radio" onchange="updateImage(this.id);">
        <label class="sub-label" for="'.$case_id.'">Element with ID 2</label>
      </li>
      
      <li>
        <input class="selectedoptions" id="3" name="config-case" value="'.$case_price.'" type="radio" onchange="updateImage(this.id);">
        <label class="sub-label" for="'.$case_id.'">Element with ID 3</label>
      </li>
    
    </ul>
    
    <input class="selectedoptions" id="32" name="config-case" value="24"
    type="radio" onchange="updateImage(this.id);">

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看