【问题标题】:Set Value for HTML Dropdown based on Another HTML Dropdown Selection根据另一个 HTML 下拉选择设置 HTML 下拉列表的值
【发布时间】:2016-12-12 18:27:16
【问题描述】:

我有一个html下拉列表,即选择值时,它会显示一个与选择对应的表。我也有一个添加按钮。添加按钮内有两个需要填写的信息字段,一个是下拉框。这是我需要但无法弄清楚的。我希望我的添加按钮下拉列表中的选定值是第一个下拉列表中已经选择的数字。我该怎么做?

这是我目前的代码:

<button class="ui-button ui-corner-all ui-widget" id="insertButton">Add Supplier ID</button><br><br>

<div id="dialog-form" title="Add Supplier ID">
  <p class="validateTips">All form fields are required.</p>

<!-- Dialog box displayed after add row button is clicked -->
  <form>
    <fieldset>
      <label for="mr_name">MR_ID</label>
      <select name="mr_id" id="mr_id_dialog" class="text ui-widget-content ui-corner-all" value="300">
          <?php foreach($user->fetchAll() as $user1) { ?>
            <option>
                <?php echo $user1['MR_ID'];?>
            </option>
        <?php } ?>
      </select><br><br>
      <label for="buyer_id">Supplier ID</label>
      <input type="text" name="supp_id" id="supplier_id" class="text ui-widget-content ui-corner-all" value="99">

      <!-- Allow form submission with keyboard without duplicating the dialog button -->
      <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

<!-- Form -->    
<form name="myForm" action="">

<!-- Dropdown List -->
    <select name="master_dropdown" id="mr_id" onChange="updatetable(this.form);">
    <option selected value="select">Choose a MR_ID</option>
        <?php foreach($users->fetchAll() as $user) { ?>
            <option data-name="<?php echo $user['MR_ID'];?>">
                <?php echo $user['MR_ID'];?>
            </option>
        <?php } ?>
    </select>
</form>

<!-- Table -->
<p> 
    <div id="table_div">
        <table border="1" id="index_table" class="ui-widget ui-widget-content" style="display: none">
            <thead>
                <tr class="ui-widget-header">
                <td>MR ID</td>
                <td>Supplier ID</td>
                </tr>
            </thead>
            <?php foreach($users_one->fetchAll() as $supp) { ?>
            <tr>
                <td class="mr_id"><div id="dropdown_selection"></div></td>
                <td class="supp_id"><?php echo $supp['Supp_ID']?></td>
            </tr>
            <?php } ?>
        </table>
    </div>

【问题讨论】:

    标签: javascript jquery html dropdown


    【解决方案1】:

    请参阅Events for Select 以获取有关带有 Select 标记的事件的信息

    这可以用 javascript 来完成。

    在第一个下拉列表中添加一个更改侦听器,并从 HTML 中删除 onChange。您可以从 javascript 中调用 updatetable 函数。

    javascript

    function bindListeners() {
        var elementToBind = document.querySelector('#mr_id');
        // Check if the element has loaded
        if(elementToBind === undefined) {
            // if not, wait 500ms and retry
            setTimeout(bindListeners, 500);         
        }
        else 
        {
            // Add the event listeners
            elementToBind.addEventListener('change', updateSelection, false);
            elementToBind.addEventListener('click', updateSelection, false);
        }
    }
    bindListeners();
    
    function updateSelection(event) {
        // Get the value from this select
        var selectedValue = event.target.value;
    
        // find the select object that you want to set the value for
        var selectObjectToSet = document.querySelector('#mr_id_dialog');
        selectObjectToSet.value = selectedValue;
    
        // Call the function that was in the onChange listener
        updatetable(this.form);
    }
    

    HTML 添加供应商 ID

    <div id="dialog-form" title="Add Supplier ID">
      <p class="validateTips">All form fields are required.</p>
    
    <!-- Dialog box displayed after add row button is clicked -->
      <form>
        <fieldset>
          <label for="mr_name">MR_ID</label>
          <select name="mr_id" id="mr_id_dialog" class="text ui-widget-content ui-corner-all" value="300">
              <?php foreach($user->fetchAll() as $user1) { ?>
                <option>
                    <?php echo $user1['MR_ID'];?>
                </option>
            <?php } ?>
          </select><br><br>
          <label for="buyer_id">Supplier ID</label>
          <input type="text" name="supp_id" id="supplier_id" class="text ui-widget-content ui-corner-all" value="99">
    
          <!-- Allow form submission with keyboard without duplicating the dialog button -->
          <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
        </fieldset>
      </form>
    </div>
    
    <!-- Form -->    
    <form name="myForm" action="">
    
    <!-- Dropdown List -->
        <select name="master_dropdown" id="mr_id" onChange="updatetable(this.form);">
        <option selected value="select">Choose a MR_ID</option>
            <?php foreach($users->fetchAll() as $user) { ?>
                <option data-name="<?php echo $user['MR_ID'];?>">
                    <?php echo $user['MR_ID'];?>
                </option>
            <?php } ?>
        </select>
    </form>
    
    <!-- Table -->
    <p> 
        <div id="table_div">
            <table border="1" id="index_table" class="ui-widget ui-widget-content" style="display: none">
                <thead>
                    <tr class="ui-widget-header">
                    <td>MR ID</td>
                    <td>Supplier ID</td>
                    </tr>
                </thead>
                <?php foreach($users_one->fetchAll() as $supp) { ?>
                <tr>
                    <td class="mr_id"><div id="dropdown_selection"></div></td>
                    <td class="supp_id"><?php echo $supp['Supp_ID']?></td>
                </tr>
                <?php } ?>
            </table>
        </div>
    </p>
    

    【讨论】:

    • 那么我的 HTML 应该是什么样子?
    • 只需从mr_id select中删除onChange属性,并在最后的脚本标签中添加javascript
    • 行得通!无论如何要将它放入我的 JS 而不是 HTML 代码中以保持它对我自己的清洁?
    • 创建一个文件,命名为 something.js。
    • 然后,在您的脚本标签中将其更改为
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2014-05-10
    • 2018-04-16
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多