如何冻结或禁用更新输入表单

发布于 2024-08-16 03:18:00 字数 142 浏览 4 评论 0原文

我想知道我是否可以冻结或禁用整个更新表单?我有一个输入 h:form ,其中有一个复选框。当用户选中该框时,我想冻结或禁用整个表单,以便禁止用户更改输入。

谢谢,我正在使用 JSF、Spring Web Flow、Facelets 和 Trinidad。

I wonder if it is possible for me to freeze or disable the entire update form? I have an input h:form with a check box in it. when users check the box, I would like to freeze or disable the entire form so that disallow users from changing inputs.

Thanks, and I am using JSF, Spring Web Flow, Facelets, and Trinidad.

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

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

发布评论

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

评论(3

口干舌燥 2024-08-23 03:18:00

当用户选中复选框时,您可能希望使用 javascript 将所有表单输入设置为禁用。类似于:

document.getElementById('id').disabled = true;

您可以对每个输入元素执行此操作,其中“id”是该元素的 ID。

You would want to use javascript to set all the form inputs to disabled when the user checks the checkbox. Something like:

document.getElementById('id').disabled = true;

You would do this for each input element where 'id' is the ID of that element.

请爱~陌生人 2024-08-23 03:18:00

如果您只想禁用某些输入,最好枚举它们:

function OptCheckBox(chkd) {
 if (chkd == 'y') {
  document.frm.input1.disabled = true;
  document.frm.input2.disabled = true;
 }
}

If you want to disable only certain inputs, It is a good idea to enumerate them:

function OptCheckBox(chkd) {
 if (chkd == 'y') {
  document.frm.input1.disabled = true;
  document.frm.input2.disabled = true;
 }
}
橘虞初梦 2024-08-23 03:18:00

您无法一次禁用整个表单。您确实需要禁用每个输入元素。基本上有两种方法可以实现这一目标。

第一种方法是在单击复选框时使用 Javascript 将表单提交到服务器,这样您就可以使用 JSF 组件的 disabled 属性来禁用元素。这是一个基本示例:

<h:form>
    <h:selectBooleanCheckbox value="#{bean.freeze}" onclick="submit()" />
    <h:inputText value="#{bean.value1}" disabled="#{bean.freeze}" />
    <h:inputText value="#{bean.value2}" disabled="#{bean.freeze}" />
    <h:inputText value="#{bean.value3}" disabled="#{bean.freeze}" />
</h:form>

这里 #{bean.freeze} 应指向 boolean 属性。

第二种方法是为此编写一个 Javascript 函数。这不需要表单提交,并且可以避免一个 HTTP 请求-响应周期,这有利于用户体验。

<h:form>
    <h:selectBooleanCheckbox onclick="disableForm(this)" />
    <h:inputText value="#{bean.value1}" />
    <h:inputText value="#{bean.value2}" />
    <h:inputText value="#{bean.value3}" />
</h:form>

JS 函数 disableForm() 基本上很简单。只需通过 this 将复选框作为函数参数传入,这样您就可以通过 checkboxElement.form 获取父表单,然后通过 form.elements< 获取所有表单元素/代码>。您只需要确保没有禁用复选框本身,这样您就可以再次重新启用表单:)

function disableForm(checkboxElement) {
    var elements = checkboxElement.form.elements;
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (element != checkboxElement) {
            element.disabled = checkbox.checked;
        }
    }
}

无需事先知道 ID,这使得 JS 代码通用且可重用。

You cannot disable an entire form at once. You really need to disable each of the input elements. There are basically two ways to achieve this.

First way is to use Javascript to submit the form to the server when the checkbox is clicked, so that you can use JSF component's disabled attribute to disable the elements. Here's a basic example:

<h:form>
    <h:selectBooleanCheckbox value="#{bean.freeze}" onclick="submit()" />
    <h:inputText value="#{bean.value1}" disabled="#{bean.freeze}" />
    <h:inputText value="#{bean.value2}" disabled="#{bean.freeze}" />
    <h:inputText value="#{bean.value3}" disabled="#{bean.freeze}" />
</h:form>

Here #{bean.freeze} should point to a boolean property.

Second way is to write a Javascript function for this. This does not require a form submit and saves you from one HTTP request-response cycle, which is better for user experience.

<h:form>
    <h:selectBooleanCheckbox onclick="disableForm(this)" />
    <h:inputText value="#{bean.value1}" />
    <h:inputText value="#{bean.value2}" />
    <h:inputText value="#{bean.value3}" />
</h:form>

The JS function disableForm() is basically simple. Just pass the checkbox in as function argument by this so that you can get the parent form by checkboxElement.form and then get all form elements by form.elements. You only need to make sure that you don't disable the checkbox itself, so that you could re-enable the form again :)

function disableForm(checkboxElement) {
    var elements = checkboxElement.form.elements;
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (element != checkboxElement) {
            element.disabled = checkbox.checked;
        }
    }
}

No need to know the ID's beforehand and this makes the JS code generic and reuseable.

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