javascript 使选择的下拉列表只读(未禁用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30620563/
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
Making Chosen Dropdown Read only (not disabled)
提问by Robin
I'm looking to make a chosendropdown menu read only, and I need to do this dynamically using a jQuery selector. If I set it to disabled using:
我希望将选定的下拉菜单设为只读,我需要使用 jQuery 选择器动态执行此操作。如果我使用以下方法将其设置为禁用:
$("#dropdownDepartment").attr("disabled","disabled").trigger('chosen:updated');
the value isn't POSTed but I need it to be.
该值未发布,但我需要它。
I have tried
我努力了
$("#dropdownDepartment").attr('readonly',true).trigger('chosen:updated');
but this doesn't work.
但这不起作用。
I'm using Chosen v1.4.2
and jQuery v2.1.4
.
我正在使用Chosen v1.4.2
和jQuery v2.1.4
。
Help appreciated! Thank you.
帮助表示赞赏!谢谢你。
回答by Илья Стельмаков
The best solution for me was such an ugly trick $("#dropdownDepartment").prop('disabled',true).trigger('chosen:updated').prop('disabled',false)
.
So chosen is disabled but value from select is POSTed.
对我来说最好的解决办法就是这样一个丑陋的把戏$("#dropdownDepartment").prop('disabled',true).trigger('chosen:updated').prop('disabled',false)
。所以选择被禁用但来自选择的值被发布。
回答by George
There are a few options you could do.
您可以选择几个选项。
- Disable the drop down and store the value in a hidden field and send that in a POST.
Disable all the other selections so only the one selected is enabled
$('#dropdownDepartment option:not(:selected)').attr('disabled', true);
- Disable the drop down and before you do a post enable it
- When you want to disable it turn it into a read only textbox
- 禁用下拉菜单并将值存储在隐藏字段中,然后在 POST 中发送。
禁用所有其他选择,以便仅启用选定的一个
$('#dropdownDepartment option:not(:selected)').attr('disabled', true);
- 禁用下拉菜单,然后在发帖之前启用它
- 当你想禁用它时,把它变成只读文本框
回答by user3819192
回答by Siddiqui Noor
I have a workaround at the same. So hope it will help someone in the future (I guess you already did the solution).
我也有一个解决方法。所以希望它会在未来对某人有所帮助(我猜你已经做了解决方案)。
First print the selected option value of the chosen select into a hidden input like the following. For $_POST
value get the value from this hidden input $_POST['myselect']
:
首先将所选选择的所选选项值打印到如下所示的隐藏输入中。对于$_POST
value 从这个隐藏输入获取值$_POST['myselect']
:
<input type="hidden" name="myselect" value="selected_option_value_goes_here" />
Then disable the chosen select. But it is safe to set the hidden input value to the current one before disable it:
然后禁用所选的选择。但是在禁用它之前将隐藏的输入值设置为当前值是安全的:
$('input[name=myselect]').val($(".chosen-select").val());
$('.chosen-select').attr("disabled", true).trigger("chosen:updated");
N.B. If you don't want to disable it then just no need to update the value of the hidden input on change event of the chosen select.
注意如果您不想禁用它,则无需在所选选择的更改事件上更新隐藏输入的值。
回答by Soader03
I may be late to the party, but I came across the same issue as OP. The workaround I have found to post back the values even if they are disabled is to re-enable them when the form is submitted.
我可能会迟到,但我遇到了与 OP 相同的问题。我发现即使它们被禁用也回发这些值的解决方法是在提交表单时重新启用它们。
$("form").bind("submit", function () {
$("#TheList *").prop("disabled", false).trigger("chosen:updated");
});
Notice the [space][star] after TheList, it is to recursively re-enable each child in the list.
注意TheList后面的[space][star],就是递归地重新启用列表中的每个孩子。