javascript - 复制突出显示的元素
问题描述
解决方案
要获取index
所选option
元素的 ,请使用该selectedIndex
属性。
要将选项附加到目标,请select
使用该appendChild()
方法。
要从源中删除选项,请select
使用该removeChild()
方法。
var source_select = document.getElementById("roles");
var target_select = document.getElementById("asgndroles");
function copy_selected() {
var selected_option = source_select.options[source_select.selectedIndex];
source_select.removeChild(selected_option);
target_select.appendChild(selected_option);
}
<select size="5" id="roles">
<option>Admin</option>
<option>User</option>
<option>Super User</option>
<option>Super Admin</option>
</select>
<select size="5" id="asgndroles">
</select>
<button onclick="copy_selected()">COPY</button>
推荐阅读
- php - 如何像 PHP 中的框架一样传递参数?
- vhdl - 使用 VHDL 的 RTL 硬件设计,示例 7.1
- elasticsearch - 如何从 Spark Structured Streaming 更新 ElasticSearch 中的计数器?
- ios - 骨架库 - 带有约束的“数组”类型的扩展不能有继承子句
- mysql - 将原始 SQL 查询转换为 Laravel DB 查询
- python - curl:(7)无法连接到 localhost 端口 9200:连接被拒绝 [Ubuntu 16.04,ES 2.3.4.1,Java 9.0.1]
- xamarin.forms - Xamarin 形成 FFImageLoading CornersTranformation
- php - 在同一页面上登录和注册表单 Laravel 5.6
- java - 如何找到 javax.transaction.RollbackException 的原因?
- azure - 有没有办法以编程方式创建 Azure Active Directory 实例?