简体   繁体   English

如何禁用输入字段而不影响PHP表单中字段的值?

[英]How to disable input field without affecting the value of the field in a PHP form?

I'm creating a demo page where certain setting fields have to be disabled.我正在创建一个演示页面,其中必须禁用某些设置字段。 I have tried disabling the input with the value remaining intact, just greyed out.我试过禁用输入,值保持不变,只是变灰了。 I have disabled the inputs using disabled="true".我已经使用 disabled="true" 禁用了输入。 When submitting the form, the value disappears in spite of being there before.提交表单时,尽管之前存在该值,但该值消失了。 How do I prevent the value from disappearing whilst simultaneously disabling the said fields?如何在禁用所述字段的同时防止值消失?

If you want the value to be displayed and and not changed , you can use readonly如果您希望显示值而不更改值,则可以使用 readonly

<input type="text" name="xxx" value="xxx" readonly="readonly" />

If you want the value to be hidden and submitted to the action file you can use type =hidden如果您希望隐藏该值并提交到操作文件,您可以使用 type =hidden

<input type="hidden" name="xxxx" value="xxx" /> 

More about HTML input tag can be found here http://www.w3schools.com/tags/tag_input.asp有关 HTML 输入标签的更多信息,请访问 http://www.w3schools.com/tags/tag_input.asp

disabled form fields are not submitted under any circumstances.在任何情况下都不会提交disabled表单字段。

The most common way to avoid this problem is making them readonly and adding an input[readonly] { ... } css rule to make the text gray like in a disabled field.避免此问题的最常见方法是将它们设置为readonly并添加input[readonly] { ... } css 规则以使文本像禁用字段中的一样变灰。

You might also want to use some JavaScript to prevent it from being focused;你可能还想使用一些 JavaScript 来防止它被聚焦; could look like this if you have jQuery:如果你有 jQuery,可能看起来像这样:

$('input[readonly]').live('focus', function(e) {
    $(this).blur();
});

Just throwing suggestions around here.只是在这里提出建议。
You could disable the field as normal with Javascript, but before the field get's disabled you could use Javascript to input the value of that field into a hidden field.您可以像往常一样使用 Javascript 禁用该字段,但在禁用该字段之前,您可以使用 Javascript 将该字段的值输入到隐藏字段中。

function runme() {
    var newval = $('#field1').val();
    $('#hiddenfield').val(newval);
    $('#field1').attr('disabled', 'disabled');
}

<input id="field1" name="field1" type="text" value="this val" />
<input id="hiddenfield" name="hiddenfield" type="hidden" />
<input type="button" onclick="runme();" />

use readonly attribute instead of disabling使用只读属性而不是禁用

another approach would be to store values in PHP session and keep them on the server.另一种方法是将值存储在 PHP 会话中并将它们保存在服务器上。

For input type="file" make sure you do this:对于 input type="file" 确保你这样做:

<style>
input[type=file][readonly] {
    pointer-events: none;
}
</style>

<script>
    $('input[type="file"]').prop('readonly', true);
</script>

You could also use a hidden field of the same name with the correct value and retain your disabled field.您还可以使用具有正确值的同名隐藏字段并保留禁用字段。 Although do not trust 100% the data in a hidden / read only field as they could still be changed (through web dev tools or similar)尽管不要 100% 信任隐藏/只读字段中的数据,因为它们仍然可以更改(通过 Web 开发工具或类似工具)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM