简体   繁体   English

如何在AJAX调用后使用javascript设置选择下拉列表的默认值

[英]How to set the default value on select dropdowns with javascript after AJAX call

I have a program that generates a table rows based on part selections from a dropdown menu. 我有一个程序,根据下拉菜单中的部分选择生成表行。 On each row that is created, I have a <td> element that contains a dropdown for quantity. 在创建的每一行上,我有一个<td>元素,其中包含数量下拉列表。 My problem is that when a new part is selected thus creating a new row, all my quantity dropdowns reset back to the default value of 1. I tried fixing it with "selected = true;" 我的问题是,当选择一个新部件从而创建一个新行时,我的所有数量下拉菜单都会重置为默认值1.我尝试用“selected = true;”修复它 but that doesn't seem to work. 但这似乎不起作用。 Basically, I need to set the selected value of each quantity dropdown to stay as whatever selected value it was last set at when the user adds a new row. 基本上,我需要将每个数量下拉列表的选定值设置为当用户添加新行时最后设置的任何选定值。 I'm using an ajax call to generate the data from a SQL table on each new row. 我正在使用ajax调用从每个新行的SQL表生成数据。

Here is the function which is called from the select dropdown. 这是从select下拉列表中调用的函数。 I've omitted a lot of unnecessary code: 我省略了很多不必要的代码:

function update(){
    var selectLists = document.getElementsByName("qty_dropdown");
    for(var i = 0; i < selectLists.length; i++){
        var firstSelectList = selectLists[i].value;    
    }
}

Here is my dropdown: 这是我的下拉列表:

<td>
    <select name = "qty_dropdown" onChange = "update()"> 
        <option value = '1'> 1 </option>  
        <option id = '2'value = '2'> 2 </option> 
        <option value = '3'> 3</option> 
        <option value = '4'> 4 </option> 
        <option value = '5'> 5 </option> 
        <option value = '6'> 6 </option> 
        <option value = '7'> 7 </option> 
        <option value = '8'> 8 </option> 
        <option value = '9'> 9 </option> 
        <option value ='10'> 10</option> 
    </td>
</select> 

The function you are performing inside update() (i renamed it to add() ) should be performed on the addition of new row. 您在update()中执行的函数(我将其重命名为add())应该在添加新行时执行。

function add(){
    var selectLists = document.getElementsByName("qty_dropdown");
    var firstSelectList=[];
    for(var i = 0; i < selectLists.length; i++){
      firstSelectList.push(selectLists[i].value);
    }
    document.getElementById("inject").innerHTML+="<select name = 'qty_dropdown' onChange = 'update()'>           <option value = '1'> 1 </option>            <option id = '2'value = '2'> 2 </option>           <option value = '3'> 3</option>           <option value = '4'> 4 </option>           <option value = '5'> 5 </option>           <option value = '6'> 6 </option>           <option value = '7'> 7 </option>           <option value = '8'> 8 </option>           <option value = '9'> 9 </option>           <option value ='10'> 10</option>           </select>     ";
    for(var i = 0; i < selectLists.length-1;i++){
    selectLists[i].value=firstSelectList[i];
    }
}

this function add() will add a new row without resetting previous dropdown. 此函数add()将添加一个新行而不重置上一个下拉列表。

Demo : https://jsfiddle.net/0wur28ot/ 演示: https//jsfiddle.net/0wur28ot/

What i did is save the value in array before adding new row and then i assigned values to the dropdown accordingly after row addition. 我做的是在添加新行之前保存数组中的值,然后在添加行之后相应地为下拉列表赋值。

Here is one way you can do it. 这是你可以做到的一种方式。 You can keep a variable around that keeps track of the most recently selected dropdown value, and when you add a new dropdown to the DOM, it will use that value as its initial value. 您可以保留一个变量来跟踪最近选择的下拉值,当您向DOM添加新下拉列表时,它将使用该值作为其初始值。

 var lastUpdatedDropdownValue = ""; function update(event) { lastUpdatedDropdownValue = event.target.value; } function addNewDropdown() { var dropdown = document.querySelector("[name=qty_dropdown]"); var newDropdown = dropdown.cloneNode(true); // make sure no duplicate IDs. If your elements that are being cloned have IDs, remove them before re-inserting into the DOM document.getElementById("dropdownContainer").append(newDropdown); var selectedOption = newDropdown.querySelector("[value='" + lastUpdatedDropdownValue + "']"); selectedOption.selected = true; } 
 <button onclick="addNewDropdown()">Add New Dropdown</button> <div id="dropdownContainer"> <select name="qty_dropdown" onChange="update(event)"> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> <option value='4'>4</option> <option value='5'>5</option> <option value='6'>6</option> <option value='7'>7</option> <option value='8'>8</option> <option value='9'>9</option> <option value='10'>10</option> </select> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM