jQuery Twitter bootstrap select readonly 仍然可以更改选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32846865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twitter bootstrap select readonly still able to change options
提问by Ruben
I have a bootstrap select with readonly="true"
but I can still change the selected option.
我有一个引导选择,readonly="true"
但我仍然可以更改选定的选项。
I need the disabled="true"
behavior, but when I use this the select is not submitted.
我需要这种disabled="true"
行为,但是当我使用它时,不会提交选择。
I need a combination of the 2, the selected option can not be changed but the select has to be submitted.
我需要两者的组合,无法更改所选选项但必须提交选择。
I could use hidden fields, but I was hoping for an easier solution.
我可以使用隐藏字段,但我希望有一个更简单的解决方案。
采纳答案by BHoft
Another possibility could be to use a select replacement with dropdowns there it should be possible to disable the dropdown but still submitting the value of the hidden select element. But then you could also use hidden fields...
另一种可能性是使用带有下拉列表的选择替换,应该可以禁用下拉列表但仍提交隐藏的选择元素的值。但是你也可以使用隐藏字段......
Or you really add a hidden field with the selected value in case the select element is disabled.
或者你真的添加了一个带有选定值的隐藏字段,以防选择元素被禁用。
<input type="hidden" name="whatever">
<select name="whatever">
If the hidden field has the same name as the select, the selected value of the select should overwrite the submitted value of the hidden field (e.g. when the select field is dynamically enabled with js). And if the select is disabled the value of the hidden field is sent. Not sure about the order of what is submitted first.
如果隐藏字段与select同名,select的selected值应该覆盖隐藏字段提交的值(例如当select字段用js动态启用时)。如果选择被禁用,则发送隐藏字段的值。不确定首先提交的顺序。
$(document).ready(function(){$('select option:not(:selected)').attr('disabled',true);});
this solution should also work (at least with js enabled). It disable every not selected option. And therefore the selected option is submitted and it couldn't be changed.
这个解决方案也应该有效(至少在启用 js 的情况下)。它禁用每个未选择的选项。因此,所选选项已提交且无法更改。
There is a similar and already answered question html-form-readonly-select-tag-input
有一个类似的并且已经回答的问题html-form-readonly-select-tag-input
回答by Sean Wessell
I've run into a similar issue and what i do is when the form onsubmit event is fired remove the disabled attributes as the browser will not post disabled attributes back to the server.
我遇到了类似的问题,我所做的是在触发表单 onsubmit 事件时删除禁用的属性,因为浏览器不会将禁用的属性发布回服务器。
$('form').submit(function () { $('[disabled]').removeAttr('disabled'); })
As you mentioned the only other option i have found is to find the values to hidden inputs.
正如您提到的,我发现的唯一其他选择是找到隐藏输入的值。
回答by Yush0
At pageload add disabled to readonly
在页面加载时将禁用添加到只读
$('[readonly]').prop( "disabled", true );
Before Submit remove disabled
提交前删除禁用
$('[readonly]').prop( "disabled", false );
Note from Jquery doc:
来自 Jquery 文档的注释:
As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.
从 jQuery 1.6 开始,.attr() 方法为尚未设置的属性返回 undefined。要检索和更改 DOM 属性,例如表单元素的选中、选中或禁用状态,请使用 .prop() 方法。