简体   繁体   English

JS中的“需要解决方法”:对象不是函数(调用form.submit()时)

[英]Need Workaround in JS: object is not a function (when calling form.submit())

The legacy system I have to work with is a disgusting mixture of PHP and Java. 我必须使用的旧系统是PHP和Java的令人讨厌的混合物。 HTML is generated by both. 两者都生成HTML。 Processing form submits is generally done in the Java, such as in the case of this form. 处理表单提交通常是在Java中完成的,例如在这种表单的情况下。 In the current form the server-side expects an element named "submit" when it validates the form input. 在当前形式中,服务器端在验证表单输入时期望一个名为“ submit”的元素。 Otherwise it just reloads the form page. 否则,它只会重新加载表单页面。

This was fine until management decided they wanted a form that added a checkbox to confirm some legal stuff and wanted it to be validated on the front-end before being submitted. 这很好,直到管理层决定他们想要一个添加了复选框以确认一些合法内容的表格,并希望在提交之前先在前端对其进行验证。 This is when all heck broke loose. 这是所有的东西都松开的时候。

You see in order to validate on the front-end I need to override the basic HTML submit on button click and direct it to a validation function. 您看到为了在前端进行验证,我需要重写按钮单击时的基本HTML提交并将其定向到验证功能。 Then use JS to call the form.submit() function. 然后使用JS调用form.submit()函数。 Unfortunately in the DOM this functionality is overridden by any element in the form by the name of "submit". 不幸的是,在DOM中,此功能被“提交”名称中的任何形式的元素所覆盖。

This is creating the following error: "Uncaught TypeError: object is not a function". 这将导致以下错误:“未捕获的TypeError:对象不是函数”。

Obviously, this is due to having a submit button with the name of "submit". 显然,这是由于具有名为“ submit”的提交按钮。

The problem is that I don't think we can edit the Java side of this to not expect that (I will find out more tomorrow). 问题在于,我认为我们不能在Java方面进行修改以至于不期望这样做(我明天会发现更多内容)。 This is due to an archaic and overly complex, horrible system that breaks when you sneeze too hard. 这是由于一个古老而又过于复杂,可怕的系统造成的,当您打喷嚏时它会破裂。

So I need a workaround on the front-end that will allow me to submit a form input with the name of "submit" and a value of "Submit" AND still be able to use JS validation. 因此,我需要在前端提供一种解决方法,使我能够提交名称为“ submit”且值为“ Submit”的表单输入,并且仍然可以使用JS验证。

Sample code: 样例代码:

<form class="disclosure-agreements" method="post" action="/" name="form_a">
    <input type="hidden" name="page" value="nda_agree">
    <input type="hidden" name="nda_agree_page" value="true">
    <input type="hidden" name="nda_agreed" value="true">

    <div style="margin-right: 10px; width: 410px; font-size: 10px;">
        <div class="blue-checkbox">
            <input type="checkbox" id="disclosure-agree" name="disclosure-agree">
            <label for="disclosure-agree">
                <span class="strong green-text large-text">I AGREE</span> By clicking this box to submit my application, I certify that I have read and agreed to all of the terms, conditions, provisions and disclosures contained in the blah blah blah
            </label>
        </div>
    </div>

    <div class="nda_btns">
        <div id="btn-wrapper" class="center">
            <button id="submit-btn" name="submit" class="green-bkgd" tabindex="" value="Submit" onclick="return false;" type="submit">
                <span class="primary-btn-text">Submit</span><br>
                <span class="sub-btn-text">Apply Now</span>
            </button>

            <button id="cancel-btn" name="cancelbutton" class="red-bkgd" onclick="return false;">
                <span class="primary-btn-text">Cancel</span><br>
                <span class="sub-btn-text">Do Not Apply</span>
            </button>
        </div>
    </div>
</form>

With this JS: 有了这个JS:

<script type="text/javascript">
$(document).ready(function() {
    $('form.disclosure-agreements #submit-btn').click(function() {
        if ($('form.disclosure-agreements input#disclosure-agree').is(':checked') === true) {
            $('form.disclosure-agreements').submit();
            //document.createElement('form').submit.call(document.form_a); //I tried this too
            return true;
        } else {
            alert("You must read and agree to the terms and disclosures before submitting.");
            return false;
        }
    });
});
</script>

remove the onclick="return false;" 删除onclick="return false;" for the button and the call of $('form.disclosure-agreements').submit(); 按钮和$('form.disclosure-agreements').submit();的调用$('form.disclosure-agreements').submit();

When you click the button and the click-handler returns false it's sufficient(the click will be cancelled and the form will not be submitted) 当您单击按钮并且单击处理程序返回false就足够了(单击将被取消,并且将不提交表单)

When the click -handler returns true the form will be submitted(that's the default-action when you click a submit-button, there's no need to call submit() ) click -handler返回true ,将提交表单(这是单击“提交”按钮时的默认操作,无需调用submit()

The object is not a form, try something like this: 该对象不是表单,请尝试如下操作:

$('form').submit();

**or better in your case ** **或更适合您的情况**

$(this).parents('form').submit(); // much safer to use no names, it is shorter and faster

this = the button you click on 这=您单击的按钮

Also return only false when checkbox is not checked, do not return true. 如果未选中复选框,也仅返回false,不返回true。 use return; 使用return; To cancel the event you need to do the following: 要取消活动,您需要执行以下操作:

function(e)
{
   if( <checked> ) {
     <submit form>
     return;
   }  

   try {
     e.preventDefault();
     e.stopImmediatePropagation();
     e.stopPropagation();
  } catch(ee) {}  
  return false;
} 

But a better idea is to use the 'submit' event of the form $('form').on('submit', function(e) {} ); 但是更好的主意是使用$('form').on('submit', function(e) {} );形式的'submit'事件$('form').on('submit', function(e) {} ); and remove the onclick event handlers. 并删除onclick事件处理程序。

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

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