根据另一个HTML下拉选择设置HTML下拉列表的值

时间:2016-12-12 18:27:16

标签: javascript jquery html dropdown

我有一个HTML下拉列表,当选择一个值时,它会显示一个与选择对应的表。我也有一个添加按钮。添加按钮内部是需要填写的2个信息字段,一个是下拉框。这是我需要但却无法弄清楚的。我希望“添加按钮”下拉列表中的选定值为第一个下拉列表中已选择的数字。我怎么能这样做?

这是我到目前为止的代码:

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

1 个答案:

答案 0 :(得分:2)

有关选择标记

的事件的信息,请参阅Events for 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>