removeAttr("selected") 和 .attr('selected','selected') 无法正常工作
removeAttr("selected") and .attr('selected','selected') not working properly
我的表单中有两个 select 字段。第一个是单-select,另一个是多-select。现在我想要做的是 select 多个选项 select 中的给定数据取决于单个 select 中选择的选项。
为此,我在更改单个 select 时触发 ajax-请求:
$.ajax({
type : 'POST',
url : '/Controller/getMultipleSelectIds',
data : {
singleSelectId: singleSelectId
},
success : function(data) {
var multipleSelectIds= JSON.parse(data);
$("#multipleSelectFieldId option:selected").prop("selected", false);
$("#multipleSelectFieldId option").each(function()
{
if ($(this).val() in multipleSelectIds) {
$(this).attr('selected','selected');
}
});
$("#multipleSelectFieldId").trigger("chosen:updated");
}
});
假设我在数据库中有这样的值,用于单个-select-Ids:
array(
"ssId1" => array(1,2,4,5),
"ssId2" => array(1,3,5)
)
现在我选择单项-select ("ssId1") 中的第一个条目,一切正常,这意味着多项-select 得到了应有的填充(使用具有给定值 1、2、4 和 5 的选项)。
但是当我再次将 single-select 更改为另一个选项 ("ssId2") 时,选择的-select 只显示选项 3(这是一个不是 "ssId1").这一直持续到我在选择的 multiple-select.
中没有更多选项为止
我已经检查过,隐藏的 multiple-select 字段中的选项设置正确。所以现在的问题是:这是所选插件中的错误还是我做错了什么?
编辑
试图找到问题并为此使来源 select 可见。 removeAttr
或 .prop("selected", false)
甚至 .attr('selected','selected') 似乎有问题。当我在 single-select 的两个选项之间切换时,只有选项 3 被标记(蓝色背景)但是所有 3 个选项在 html 中都有 selected 属性代码。当我尝试通过 IE 11 中的 js/jquery select 多个 select(必填)字段中的所有选项并提交表单时,我遇到了类似的问题 IE 表示没有选项选择。我做错了什么?
只需在所有句子中使用 prop()
方法,它适用于 selected
等动态属性
看到你的 fiddle 修改没有问题:
https://jsfiddle.net/0aaxk2hz/3/
$(this).prop('selected', true); // add it
$(this).prop('selected', false); // remove it
我的表单中有两个 select 字段。第一个是单-select,另一个是多-select。现在我想要做的是 select 多个选项 select 中的给定数据取决于单个 select 中选择的选项。 为此,我在更改单个 select 时触发 ajax-请求:
$.ajax({
type : 'POST',
url : '/Controller/getMultipleSelectIds',
data : {
singleSelectId: singleSelectId
},
success : function(data) {
var multipleSelectIds= JSON.parse(data);
$("#multipleSelectFieldId option:selected").prop("selected", false);
$("#multipleSelectFieldId option").each(function()
{
if ($(this).val() in multipleSelectIds) {
$(this).attr('selected','selected');
}
});
$("#multipleSelectFieldId").trigger("chosen:updated");
}
});
假设我在数据库中有这样的值,用于单个-select-Ids:
array(
"ssId1" => array(1,2,4,5),
"ssId2" => array(1,3,5)
)
现在我选择单项-select ("ssId1") 中的第一个条目,一切正常,这意味着多项-select 得到了应有的填充(使用具有给定值 1、2、4 和 5 的选项)。 但是当我再次将 single-select 更改为另一个选项 ("ssId2") 时,选择的-select 只显示选项 3(这是一个不是 "ssId1").这一直持续到我在选择的 multiple-select.
中没有更多选项为止我已经检查过,隐藏的 multiple-select 字段中的选项设置正确。所以现在的问题是:这是所选插件中的错误还是我做错了什么?
编辑
试图找到问题并为此使来源 select 可见。 removeAttr
或 .prop("selected", false)
甚至 .attr('selected','selected') 似乎有问题。当我在 single-select 的两个选项之间切换时,只有选项 3 被标记(蓝色背景)但是所有 3 个选项在 html 中都有 selected 属性代码。当我尝试通过 IE 11 中的 js/jquery select 多个 select(必填)字段中的所有选项并提交表单时,我遇到了类似的问题 IE 表示没有选项选择。我做错了什么?
只需在所有句子中使用 prop()
方法,它适用于 selected
看到你的 fiddle 修改没有问题:
https://jsfiddle.net/0aaxk2hz/3/
$(this).prop('selected', true); // add it
$(this).prop('selected', false); // remove it