如何从选择 jsp 上的映射中删除选项
How to remove options from map on selection jsp?
<form:select id="name1"
<form:options items="${questionsMap}"/>
</form:select>
<form:select id="name2"
<form:options items="${questionsMap}"/>
</form:select>
<form:select id="name3"
<form:options items="${questionsMap}"/>
</form:select>
问题图来自枚举。任何人都可以帮忙,在第一次选择问题后,我如何从问题映射中删除问题,并在第二次显示未选择的问题。
你不能在JSP上这样做,因为那里的代码将在服务器端运行 - 如果你不想对服务器进行额外的查询......所以最好的方法是用JavaScript来做。Jquery 选择器使这项任务变得容易。
建议:http://api.jquery.com/remove/
此外,您可能感兴趣的事件:http://api.jquery.com/change/
在那里你甚至可以找到一个作为例子...
只是把我的例子放在这里:
$( "select" ).change(function () {
$( "select option:selected" ).each(function() {
$( this ).remove();
});
})
无需从下拉列表中删除选项,您只需验证所选的相同选项并在提交时显示错误消息。
var validateSubmit = function() {
var errors = [];//trace all errors here
//fetch selected values from the drop-down starting name attribute with `name`
var txt = $('select[name^=name]').map(function() {
return this.value; //get current drop-down selected value
}).get(); //get all value in array
$.each(txt, function(i, v) {//loop in the selected values
if (i !== $.inArray(v, txt)) {//inArray, gives -1 if not found else array index
errors.push('Cannot select [' + v + '] more than once');
}
});
var submit = 0 === errors.length//true if no errors, else false
if (!submit) {
alert(errors.join(''n'));//concat all errors with 'n
}
return submit;//submit if true, else don't submit
};
$(function() {
$('#form1').on('submit', validateSubmit);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id='form1'>
<select name='name1'>
<option>JS</option>
<option>SQL</option>
<option>XML</option>
</select>
<select name='name2'>
<option>JS</option>
<option>SQL</option>
<option>XML</option>
</select>
<select name='name3'>
<option>JS</option>
<option>SQL</option>
<option>XML</option>
</select>
<button>Submit</button>
</form>
或
您可以简单地验证所选的相同选项,并在更改时显示错误消息。
var names;
var validateChange = function() {
var errors = []; //trace all errors here
//fetch selected values from the drop-down starting name attribute with `name`
var txt = names.map(function() {
return this.value; //get current drop-down selected value
}).get(); //get all value in array
$.each(txt, function(i, v) { //loop in the selected values
if ('' !== v && i !== $.inArray(v, txt)) { //inArray, gives -1 if not found else array index
errors.push('Cannot select [' + v + '] more than once');
}
});
if (errors.length) {
alert(errors.join(''n')); //concat all errors with 'n
}
};
$(function() {
names = $('select[name^=name]');
names.on('change', validateChange);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select name='name1'>
<option value=''>-- Select --</option>
<option>JS</option>
<option>SQL</option>
<option>XML</option>
</select>
<select name='name2'>
<option value=''>-- Select --</option>
<option>JS</option>
<option>SQL</option>
<option>XML</option>
</select>
<select name='name3'>
<option value=''>-- Select --</option>
<option>JS</option>
<option>SQL</option>
<option>XML</option>
</select>
这是我制作的jquery代码,它正是这样做的。还会保留空值(如果顶部有"请选择"(,并重新插入未选择的值。它对页面上的所有选择元素执行此操作,如果您需要限制将选择器 $("select"( 替换为更具体的选择器:
$( document ).ready(function() {
manageSelectValues();
$("select").change(manageSelectValues);
});
function manageSelectValues()
{
$("select").each(function() {
var id = $(this).attr('id');
var value = $(this).find(":selected").val();
if (value) {
$("select[id!='" + id + "']").find("option").filter(function() { return this.value == value;}).remove();
}
$(this).find("option").filter(function() { return this.value != value;}).each(function() {
var unselValue = $(this).val();
$("select").filter(function() { return $(this).find("option").filter(function() { return this.value == unselValue;}).length == 0; }).append(
$('<option></option>').val(unselValue).html(unselValue));
})
});
}
相关文章:
- 使用php或javascript从facebook相册URL中删除多余的部分
- 如何在映射数组中添加换行符
- 从MVC中删除映射
- 从服务器中删除映射数据,丢失订阅
- 如何在 IE 11 中删除图像映射中的虚线
- 如何从选择 jsp 上的映射中删除选项
- 使用JavaScript映射reduce从数组中删除项
- KNOCKOUTJS 映射插件从对象中删除函数
- ES6:在设置/映射迭代期间从集合/映射中删除元素是否危险
- 从Javascript中的映射中删除某些元素
- 可以't删除函数的映射部分
- 在不删除未列出项目的情况下,将多个源的映射删除到单个列表中
- 如何创建哈希映射,以便我可以在javascript中循环或删除
- 我可以有一个javascript对象从内存中删除自己设置它的映射值为空
- 从映射值中删除最后一个字母
- 删除映射和foreach数据绑定表上的按钮,缺少对视图模型的引用
- 删除javascript映射的forEach循环中的条目安全吗
- JS当我使用数组作为映射时,如何从数组中删除元素
- javascript编程:如何在openlayers中删除tile映射的空白区域
- 到子数组的删除映射