使用 JavaScript 和 JQuery,如何维护动态更新的 HTML 选择元素的选定状态?
我有一个表单 UI,其中多个部分需要从单个可动态更新的选择列表动态更新重复的 HTML 选择列表。
动态更新列表工作得很好,因为可以动态添加和删除新选项。然后,我可以使用 JQuery .find() 将此更新传播到每个重复列表。我什至添加了一些逻辑来维护原始选择列表的当前选定索引。
我无法做的是在原始选择列表中添加和删除新选项时维护每个重复选择列表的选定状态。当对原始选择列表的每次更新都会迭代每个重复的选择列表时,它们会丢失当前选择的选项索引。
这是我的难题的一个示例--*编辑--我鼓励您尝试执行我在下面提供的代码,并在提出解决方案之前应用您的理论。 ,因为到目前为止所有建议都没有奏效。我相信您会发现这个问题比您一开始想象的要棘手:
<form>
<div id="duplicates">
<!--// I need for each of these duplicates to maintain their currently selected option index as the original updates dynamically //-->
<select>
</select>
<select>
</select>
<select>
</select>
</div>
<div>
<input type="button" value="add/copy" onclick="var original_select = document.getElementById('original'); var new_option = document.createElement('option'); new_option.text = 'Option #' + original_select.length; new_option.value = new_option.text; document.getElementById('original').add(new_option); original_select.options[original_select.options.length-1].selected = 'selected'; updateDuplicates();" />
<input type="button" value="remove" onclick="var original_select = document.getElementById('original'); var current_selected = original_select.selectedIndex; original_select.remove(original_select[current_selected]); if(original_select.options.length){original_select.options[current_selected < original_select.options.length?current_selected:current_selected - 1].selected = 'selected';} updateDuplicates();" />
<select id="original">
</select>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function updateDuplicates(){
$("#duplicates").find("select").html($("#original").html());
}
</script>
</form>
重要的是要注意,如果可能的话(即没有 ID),重复的 HTML 选择列表应该保持一定程度的任意性,因为此方法需要应用一般适用于整个文档中其他动态创建的选择列表。
提前致谢!
I have a form UI whereby several sections require duplicate HTML select list to be updated dynamically from a single, dynamically-updatable select list.
The dynamically-updatable list works just fine, in that new options can be added and removed on-the-fly. I can then get this update to propagate through each of the duplicate lists using JQuery .find(). I have even added a bit of logic to maintain the currently selected index of the original select list.
What I'm not able to do is maintain the selected state of each of the duplicate select lists as new options are added and removed from the original select list. As each update to the original select list iterates through each duplicate select list, they lose their currently selected option index.
Here is an example of my conundrum--*EDIT--I would encourage you to try and execute the code I've provided below and apply your theories before suggesting a solution, as none of the suggestions so far have worked. I believe you will find this problem a good deal trickier than you might assume at first:
<form>
<div id="duplicates">
<!--// I need for each of these duplicates to maintain their currently selected option index as the original updates dynamically //-->
<select>
</select>
<select>
</select>
<select>
</select>
</div>
<div>
<input type="button" value="add/copy" onclick="var original_select = document.getElementById('original'); var new_option = document.createElement('option'); new_option.text = 'Option #' + original_select.length; new_option.value = new_option.text; document.getElementById('original').add(new_option); original_select.options[original_select.options.length-1].selected = 'selected'; updateDuplicates();" />
<input type="button" value="remove" onclick="var original_select = document.getElementById('original'); var current_selected = original_select.selectedIndex; original_select.remove(original_select[current_selected]); if(original_select.options.length){original_select.options[current_selected < original_select.options.length?current_selected:current_selected - 1].selected = 'selected';} updateDuplicates();" />
<select id="original">
</select>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function updateDuplicates(){
$("#duplicates").find("select").html($("#original").html());
}
</script>
</form>
It is important to note that the duplicate HTML select lists should remain somewhat arbitrary, if at all possible (i.e.; no ID's) as this method needs to apply generically to other dynamically-created select lists throughout the document.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仍然不能 100% 确定你要问什么,但看起来这应该能满足你的要求,并且代码行数较少。
编辑:我将你的标记更改为
Still not 100% sure what you're asking but it seems like this should do what you're looking for and is a few less lines of code.
EDIT: I changed your markup to be
只需将 select 的当前选定项/值设置为某个变量,然后执行您的操作,
最后重新选择值到select。
just set the currently selected item/value of select to some variable, then do your operation,
finally reselect the value to the select.
好吧,我想我有一个可行的解决方案,即使不是一个笨拙的方法。棘手的部分不是向原始列表添加值,因为添加的选项始终位于列表的末尾。问题在于删除选择选项,因为这样做会更改当前 selectedIndex 的索引。我已经在 Mac 上使用 Google Chrome 进行了测试,没有出现任何错误。我对代码进行了注释,以演示我如何实现解决方案:
有足够的空间来修改我的代码,以便可以在当前 selectedIndex 之后插入选项,而不是附加到原始选择列表的末尾。理论上,多选列表/菜单也应该有效。来吧。
我确信这里的一位天才能够用比我的代码更干净、更漂亮的代码来完成同样的事情。感谢所有对我的原始问题进行审查和评论的人!干杯。
Okay, I think I have a workable approach to a solution, if not a clumsy one. The tricky part isn't adding a value to the original list, because the added option is always at the end of the list. The problem comes in removing a select option because doing so changes the index of the currently selectedIndex. I've tested using Google Chrome on a Mac with no errors. I have commented the code to demonstrate how I approached my solution:
There is plenty of room to modify my code so that options can be inserted after the currently selectedIndex rather than appended to the end of the original select list. Theoretically, a multi-select list/menu should work as well. Have at thee.
I'm sure one of the geniuses here will be able to do this same thing with cleaner, prettier code than mine. Thanks to everyone who reviewed and commented on my original question! Cheers.
如果您可以稍微重置一下,我认为问题是您将选择列表的 HTML 设置为另一个列表的 HTML。如果所有底层 html 都被更改,浏览器可能不会尝试保留当前选定的项目。
因此,我认为您可能会尝试做的是将选项元素显式添加到目标列表中。
试试这个jsfiddle。如果您选择默认第一项以外的项目并单击“添加”,请注意所选项目将被保留。因此,您在管理目标列表项时需要更加谨慎。
也许这会有所帮助,或者也许我错过了重点。
If you can reset a little, I think that the problem is you are setting your select list's HTML to another list's HTML. The browser probably doesn't try to preserve the currently selected item if all the of underlying html is being changed.
So, I think what you might try doing is explicitly adding the option elements to the target lists.
Try this jsfiddle. If you select an item other than the default first item and click "add", notice that the selected item is maintained. So you need to be a little more surgical in your managing of the target list items.
Maybe that'll help or maybe I missed the point.