javascript Jquery:禁用的“输入”字段在表单中不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5474992/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:19:20  来源:igfitidea点击:

Jquery : Disabled 'input' field not available in form

javascriptjquery

提问by Neo

I'm trying to dynamically set a an input value("choice" in my case) inside a form based on user inputs. After setting the desired value, I want to disable the field so that user cannot change it. Here is the code i'm using.

我正在尝试根据用户输入在表单内动态设置输入值(在我的情况下为“选择”)。设置所需的值后,我想禁用该字段,以便用户无法更改它。这是我正在使用的代码。

// HTML
<select name="0-choices" id="id_0-choices">
<option value="Phone">Phone</option>
<option value="Fax">Fax</option>
<option value="Voicemail">Voicemail</option>
</select>

//Javascript
$(cfield).children().first().removeAttr('selected');
$(cfield).children('option[value="Voicemail"]').attr('selected','selected');
//$(cfield).val('Voicemail');
$(cfield).attr('disabled','disabled');

The problem is, if I disable the field, the field is not available in the posted data. So my validation function complains of data not available. How can I "freeze" the input field and yet be able to post it.

问题是,如果我禁用该字段,则该字段在发布的数据中不可用。所以我的验证功能抱怨数据不可用。我怎样才能“冻结”输入字段,但仍然能够发布它。

回答by aleation

I got to this problem too, and guess you try to disable the fields while sending Ajax request, the best solution i found was using readonly instead:

我也遇到了这个问题,我猜你在发送 Ajax 请求时尝试禁用这些字段,我发现最好的解决方案是使用 readonly 代替:

$(cfield).attr('readonly', true);

Users can't modify the field's value but they are still read

用户不能修改字段的值,但他们仍然被读取

回答by Matt Ball

You can disable the field, and then copy its value into a hidden form field.

您可以禁用该字段,然后将其值复制到隐藏的表单字段中。

HTML

HTML

<input type="hidden" name="cfield_hidden"/>

JavaScript

JavaScript

// snip...
$(cfield).attr('disabled','disabled');
$('#cfield_hidden').val($(cfield.val());

Or, you could enable the options immediately before submitting the form:

或者,您可以在提交表单之前立即启用这些选项:

$('some-form-selector').submit(function ()
{
    $(this).find('option').attr('disabled', false);
});

回答by meo

i would clone the field on submit and set remove the disabled attr. And append it to your form as display none.

我会在提交时克隆该字段并设置删除禁用的属性。并将其作为 display none 附加到您的表单中。

Or simply remove the disabled onsubmit.

或者干脆删除禁用的提交。

PS: your username would be cooler with a M instead of the N ;)

PS:你的用户名用 M 而不是 N 会更酷;)

回答by rémy

make a hidden input field like

制作一个隐藏的输入字段,如

$('.your-form').append("<input type='hidden' name='0-choices' value='"+$('cfield option:selected).val()+"' />")

as disabled inputs won't be submitted ..

因为不会提交禁用的输入..

回答by pconcepcion

You can use a hidden field to store the value you whant to send once you disable the input field.

禁用输入字段后,您可以使用隐藏字段来存储要发送的值。

Something like this:

像这样的东西:

$(cfield).append('<input type="hidden" id="hiddenField" value="'+$(cfield).val()+'"/>');
$(cfield).attr('disabled','disabled');

回答by James Kyburz

as you say disabled inputs are not included in the post data, your best option is to either fix your server side logic or to store a input hidden field with name 0-choices and the value you want posted.

正如您所说的禁用输入不包含在发布数据中,您最好的选择是修复您的服务器端逻辑或存储名称为 0-choices 的输入隐藏字段和您想要发布的值。