基于输入值禁用按钮不适用于两种形式

发布于 2025-02-11 16:11:47 字数 1469 浏览 0 评论 0原文

首先:我正在使用WebFlow,因此问题可能与WebFlow有关。

第二,我仍在学习编码,所以请记住我仍然是我仍然一个新手。

我在一个站点上使用了两种相同的形式(因为它们是两个独立的元素),因为一个是静态的,另一个是CMS中的(集合列表)。

问题是,他们以前确实一起工作,但是我做了一些事情,而且……好吧,您知道 - 我试图揭穿发生的事情,但没有找到适当的解决方案。

出于某些非常奇怪的原因,例如,如果后者有效,例如,如果乔布斯(Jobs-form-cms-button)是第一个,那么它就不起作用,而是工作形式的按钮。反之亦然。

function checkform() {
    const formElements = document.forms["wf-form-jobs"].elements;
    let submitBtnActive = true;

    for (let inputEl = 0; inputEl < formElements.length; inputEl++) {
        if (formElements[inputEl].value.length == 0) submitBtnActive = false;
    }

    if (submitBtnActive) {
        document.getElementById("jobs-form-button").disabled = false;
    } else {
        document.getElementById("jobs-form-button").disabled = "disabled";
    }
};

function checkform() {
    const formElementsCMS = document.forms["wf-form-cms-jobs"].elements;
    let submitBtnActiveCMS = true;

    for (let inputEl = 0; inputEl < formElementsCMS.length; inputEl++) {
        if (formElementsCMS[inputEl].value.length == 0) submitBtnActiveCMS = false;
    }

    if (submitBtnActiveCMS) {
        document.getElementById("jobs-form-cms-button").disabled = false;
    } else {
        document.getElementById("jobs-form-cms-button").disabled = "disabled";
    }
};

我重命名了一些var和const,因为它没有其他工作。另外,我做了一些事情以使该代码与WebFlow一起使用,因为我无法在本机WebFlow元素中添加一些属性,例如,“ form button”(“禁用”属性是为系统保留的) - &nbsp;我通过自定义代码元素自己添加表单。

First and foremost: I’m using Webflow, so the issue can be related to Webflow somehow.

Second, I’m still learning to code, so please keep in mind that I’m still a newbie.

I’m using two same forms (different classes since, well, they are two separate elements) on a site because the one is static and another one is located in CMS (Collection List).

The thing is, they did work together before, but I did something, and… well, you know — I’ve tried to debunk what happened, but didn’t find a proper solution.

For some, very-very strange reason, only the latter works, e.g., if jobs-form-cms-button is the first, then it won’t work, but jobs-form-button will. And vice versa.

function checkform() {
    const formElements = document.forms["wf-form-jobs"].elements;
    let submitBtnActive = true;

    for (let inputEl = 0; inputEl < formElements.length; inputEl++) {
        if (formElements[inputEl].value.length == 0) submitBtnActive = false;
    }

    if (submitBtnActive) {
        document.getElementById("jobs-form-button").disabled = false;
    } else {
        document.getElementById("jobs-form-button").disabled = "disabled";
    }
};

function checkform() {
    const formElementsCMS = document.forms["wf-form-cms-jobs"].elements;
    let submitBtnActiveCMS = true;

    for (let inputEl = 0; inputEl < formElementsCMS.length; inputEl++) {
        if (formElementsCMS[inputEl].value.length == 0) submitBtnActiveCMS = false;
    }

    if (submitBtnActiveCMS) {
        document.getElementById("jobs-form-cms-button").disabled = false;
    } else {
        document.getElementById("jobs-form-cms-button").disabled = "disabled";
    }
};

I’ve renamed some vars and const because it didn’t work otherwise. Also, I did some stuff in order to make this code work with Webflow since I couldn’t add some attributes to the native Webflow elements, e.g., “Form Button” (“disabled” attribute was reserved for the system) — so I added the forms by myself via custom code element.

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

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

发布评论

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

评论(1

冷清清 2025-02-18 16:11:47

这两个函数都具有完全相同的名称,在JavaScript中,当两个函数具有相同名称时,第二个功能将覆盖第一个函数。

Both functions have the exact same name, in JavaScript when both functions have the same name, the 2nd function will override the first function.

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