简体   繁体   English

将输入表单设为只读?

[英]Make input form read-only?

I have a bunch of forms that have a lot of <input type="text"> fields on them. 我有一堆表格,上面有很多<input type="text">字段。 The user now is requiring that I provide a "read-only" version of every single form. 用户现在需要我为每个表单提供“只读”版本。 I would recode every field into an 我会将每个字段重新编码为

<xsl:choose>
<xsl:when test="/..../permission = 'E'>
  <input ....>
</xsl:when>
<xsl:otherwise>
  ...
</xsl:otherwise>
</xsl:choose>

mess, but I'm hoping for something a little more elegant. 一团糟,但我希望有一些优雅的东西。 A friend suggested 一位朋友建议

  $(function () {
        <xsl:if test="/.../permission != 'E'">
            $('input').keypress(function() { return false; });
        </xsl:if>
    });

which does part of what I want, but you can still paste into the fields and you can still delete from them. 这是我想要的一部分,但是您仍然可以粘贴到这些字段中,并且仍然可以从它们中删除。

Judging from the fact that you're using XSLT, I'd say you're outputting in one of the XHTML doctypes, so why not just make the input element you're creating have the disabled attribute? 从您正在使用XSLT的事实来看,我想说您正在输出一种XHTML文档类型,那么为什么不仅仅使您正在创建的input元素具有disabled属性呢?

<input type="..." disabled="disabled" />

You could do this immediately in XSLT. 您可以立即在XSLT中执行此操作。 If you're using the same input all over the place just create a template for it and use <xsl:apply-template .../> inside the test. 如果您在各处使用相同的输入,只需为其创建一个模板,然后在测试中使用<xsl:apply-template .../>

Post-comment edit 评论后编辑

Apparently there's a readonly attribute as well. 显然还有一个readonly属性。 Silly :) 傻:)

<input type="..." readonly="readonly" />

I've gone the simple route: 我走了简单的路线:

       $(function () {
            <xsl:if test=".../permission != 'E'">
                $('input[type="text"').attr('readonly','readonly');
            </xsl:if>
        });

I don't know why I'd never heard of the "readonly" attribute before. 我不知道为什么以前从未听说过“只读”属性。

There are differences between readonly and disabled fields- the values of readonly fields are uploaded with a form, and readonly elements can be tabbed and focused and selected, unlike disabled fields. 只读字段和禁用字段之间存在差异-只读字段的值是通过表单上传的,与禁用字段不同的是,可以对选项卡中的元素进行标签,重点突出和选择。

Also, if you change the value in straight javascript you must use 'readOnly', while html is case insensitive, and most libraries take care of it for you. 另外,如果您在直接的javascript中更改值,则必须使用“ readOnly”,而html不区分大小写,并且大多数库都会为您处理。

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

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