【问题标题】:form.submit is not a function - Angular 11form.submit 不是一个函数 - Angular 11
【发布时间】:2021-08-17 13:21:33
【问题描述】:

我遇到了几天来无法解决的问题。我正在动态创建一个表单以 POST 到外部支付 URL,但最终出现主题中的错误。这是代码的sn-p:

var form = <HTMLFormElement>document.createElement("newPlanForm");
form.method = "POST";
form.action = "https://somewebsite.comd/somepage";
form.append('Content-Type', 'application/x-www-form-urlencoded');
form.append('Accept', 'application/json');
form.style.display = 'none';

var account_number: HTMLInputElement = document.createElement("input");
account_number.value='xxxxxxxxx';
account_number.name='account_number';
form.appendChild(account_number);

document.body.appendChild(form);
form.submit();

关于如何进行的任何建议?提前谢谢你。

【问题讨论】:

    标签: angular typescript forms submit angular11


    【解决方案1】:

    我们需要做一些改变,如下所示,

    使用“form”而不是“newPlanForm”创建元素,这会导致问题。

      var form = document.createElement("form");
       form.method = "POST";
       form.action = "https://somewebsite.comd/somepage";
       form.style.display = 'none';
      
       // form['Content-Type'] =  'application/x-www-form-urlencoded';
       // form['Accept'] = 'application/json';
       // above attributes are not valid
    
    
      var account_number = document.createElement("input");
       account_number.value='xxxxxxxxx';
       account_number.name='account_number';
       form.appendChild(account_number);
    
     document.body.appendChild(form);
     form.submit();                       // will submit the form
    

    编码愉快.. :)

    【讨论】:

    • 嗨,Ganesh。感谢您的回复。但是,这导致了另一个错误:“ERROR TypeError: form.submit is not a function”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2018-02-21
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多