锁定 HTML 选择元素,允许在提交时发送值

发布于 2024-08-27 23:31:28 字数 618 浏览 3 评论 0原文

我在复杂的订单表单上有一个选择框(用于客户字段),当用户开始向订单添加行时,不应允许他们更改客户选择框(除非删除所有行)。

我立即想到的是,我可以使用禁用属性,但是当禁用该框时,所选值将不再传递到目标。

不久前出现问题时,其他开发人员之一通过循环遍历所有选项并禁用除所选选项之外的所有选项来解决此问题,果然该值已传递到目标,并且从那以后我们一直在使用。但现在我正在寻找合适的解决方案,我不想循环所有选项,因为数据正在扩展并且开始引入性能问题。

我不想在点击提交按钮时启用此/所有元素。

如何锁定输入,同时保留所选选项并将该值传递给目标脚本?如果可能的话,我更喜欢非 JavaScript 解决方案,但如果需要,我们正在运行 jQuery 1.4.2,以便可以使用。

编辑

我尝试使用 readonly 属性,但收效甚微,这是我正在使用的 jQuery 代码:

$('.abc').each(function(element) {
    $(this).attr('readonly','readonly');
});

当使用 Firebug 检查元素时,readonly 属性已被设置,但我仍然可以更改该值在选择框中?!

I have a select box (for a customer field) on a complex order form, when the user starts to add lines to the order they should not be allowed to change the customer select box (unless all lines are deleted).

My immediate thought was that I could use the disabled attribute, but when the box is disabled the selected value is no longer passed to the target.

When the problem arose a while ago one of the other developers worked around this by looping through all the options and disabling all but the selected option, and sure enough the value was passed to the target and we've been using since. But now I'm looking for a proper solution, I don't want to loop through all the options because are data is expanding and it's starting to introduce performance issues.

I'd prefer not to enable this / all the elements when the submit button is hit.

How can I lock the input, whilst maintaining the selected option and passing that value to the target script? I would prefer a non-JavaScript solution if possible, but if needed we are running jQuery 1.4.2 so that could be used.

Edit

I've tried to use the readonly attribute with little success, here's the jQuery code I'm using:

$('.abc').each(function(element) {
    $(this).attr('readonly','readonly');
});

When inspecting the element with Firebug the readonly attribute had been set, but I can still change the value in the select box?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

多情出卖 2024-09-03 23:31:28

这是可行的:

$('.abc :not(:selected)').attr('disabled','disabled');

jQuery 仍然会在幕后循环元素,但我严重怀疑你是否会遇到性能问题,除非你的选择有数千个元素(在这种情况下,你的可用性问题将远远超过性能问题)。原来的代码一定是做错了什么。

This works:

$('.abc :not(:selected)').attr('disabled','disabled');

jQuery will still be looping through the elements behind the scenes, but I seriously doubt you will have performance issues unless your select has thousands of elements (in which case your usability issues will far out weigh the performance issues). The original code must have been doing something wrong.

っ左 2024-09-03 23:31:28

这很好用

<select disabled="true">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">thre</option>
</select>

This works fine

<select disabled="true">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">thre</option>
</select>
我偏爱纯白色 2024-09-03 23:31:28

将隐藏字段添加到表单中,onsubmit 从选择中获取值并将其放入隐藏字段的值中

根据 HTML 规范,select 不支持 readonly标签

Add a hidden field to your form and onsubmit take the value from the select and place it in the hidden field's value

As per the HTML spec, readonly is not supported by the select tag

完美的未来在梦里 2024-09-03 23:31:28

该元素不接受只读属性。 Readonly 是一个解决这个问题的包装器。

试试这个:
https://github.com/haggen/readonly

The element does not accept readonly attribute. Readonly is a wrapper that fix this.

Try this:
https://github.com/haggen/readonly

别再吹冷风 2024-09-03 23:31:28

“select”没有只读属性。它只有禁用属性。

http://www.w3schools.com/TAGS/att_select_disabled.asp

所以你最好最好的是:

$('.abc').each(function(element) {
    $(this).attr('disabled','disabled');
});

HTH

"select" does not have a readonly attribute. It has only disabled attribute.

http://www.w3schools.com/TAGS/att_select_disabled.asp

So your best best is:

$('.abc').each(function(element) {
    $(this).attr('disabled','disabled');
});

HTH

少跟Wǒ拽 2024-09-03 23:31:28

禁用任何选择的一个简单方法是禁用鼠标交互。

例如:

<select id="complaint_status" name="complaint_status" class="disabledSelect" value="Pending">
<option value="Pending" selected>Pending</option>
<option value="Complete">Complete</option></select>

css

.disabledbutton {
pointer-events: none;
opacity: 0.4;}

Select 的值将被 SUBMITted。
希望它有效!

A simple way to disable any Select is to just disable mouse interaction.

For example:

<select id="complaint_status" name="complaint_status" class="disabledSelect" value="Pending">
<option value="Pending" selected>Pending</option>
<option value="Complete">Complete</option></select>

css

.disabledbutton {
pointer-events: none;
opacity: 0.4;}

The value of Select will be SUBMITted.
Hope it works!!

花间憩 2024-09-03 23:31:28

我有一个想法,这对我来说很实用:

就我而言,当用户在会计系统表单的下拉列表中选择一个选项(帐户)时,例如某种“费用”,我知道可能不是“贷方”,只是“借方”,另一个选择会计操作(借方/贷方)的下拉菜单将这些下拉菜单更改为“借方”。

然后,我“锁定”(使用“disabled=true”)“借记”选项中的最后一个“下拉菜单”。

我现在遇到的问题与您的类似:禁用下拉元素后,我无法再在目标中接收它。

所以,我做了什么:

1 - 更改了第二个下拉列表中的选项,正如我所说:

document.getElementById("operation").value = "D";

`
2 - 禁用该下拉菜单:

document.getElementById("operation").disabled = true;

然后,“cat salt”:

3a- 添加到“FORM”元素,一个“onsubmit”

onsubmit = "validForm()"

3b - 在我的 [java-script] 文件中,我添加了 [“valid-Form”] 函数:

function validForm()
{
    document.getElementById("operation").disabled = false;
}

瞧!

I'd have an idea, that was functional to me:

In my case, when a user selects an option (an account) in a drop-down on a form of an accounting system, e.g., some kind of "expense", that I know that may not be "credited", just "debited", another drop-down that selects the accounting operation (Debit/Credit), changes these drop-down to "Debit".

Then, I "lock" (using "disabled=true") these last "drop-down" in the "debit" option.

The problem that occurred to me these moment, was similar of yours: after disabling the drop-down element, I couldn't receive it in the target, anymore.

So, what I've done:

1 - Changed the option in the second drop-down list, as I said:

document.getElementById("operation").value = "D";

`
2 - Disabled that dropdown:

document.getElementById("operation").disabled = true;

Then, the "cat salt":

3a- Added to the "FORM" element, an "onsubmit"

onsubmit = "validForm()"

3b - On my [java-script] file I added the ["valid-Form"] function:

function validForm()
{
    document.getElementById("operation").disabled = false;
}

Voilá!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文