使用纯JavaScript创建,附加和提交表单 [英] Create, append and submit form using pure JavaScript

查看:56
本文介绍了使用纯JavaScript创建,附加和提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现onclick函数,该函数使用POST方法发送参数.我需要此功能来重新加载页面,这使我可以使用AJAX以外的其他方式.我有一个函数可以执行此操作,但是此函数使用jQuery.现在,我需要将其移植"到纯JavaScript.

I'm trying to implement onclick function, which send parameters using POST method. I need this function to reload the page, what makes me to use some other way than AJAX. I have a function to do this, but this function uses jQuery. Now I need to "port it" to pure JavaScript.

jQuery函数:

function domainRemoveConfirmation(id, url) {
    //trick to send javascript redirect with post data
    var form = $('<form action="' + url + '" method="post" hidden="true">' +
              '<input type="text" name="domainId" value="'+ id + '" />' +
              '</form>');
    $('body').append(form);
    $(form).submit();
}

我正在寻找纯JavaScript中的等效功能,我需要创建元素(带有输入字段的表单),将其追加并提交.

I look for the equivalent function in pure JavaScript, I need to create element (form with input fields), append it and submit it.

提前谢谢!

推荐答案

在这里:

function domainRemoveConfirmation(id, url){
    var myForm = document.createElement('form');
    myForm.setAttribute('action', url);
    myForm.setAttribute('method', 'post');
    myForm.setAttribute('hidden', 'true');
    var myInput = document.createElement('input');
    myInput.setAttribute('type', 'text');
    myInput.setAttribute('name', 'domainId');
    myInput.setAttribute('value', id);
    myForm.appendChild(myInput);
    document.body.appendChild(myForm);
    myForm.submit();
};

这篇关于使用纯JavaScript创建,附加和提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆