Javascript 按钮发布 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4666328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:32:22  来源:igfitidea点击:

Button post HTML

javascripthtmlpostdata

提问by user434885

I need to use a button without using a form. How do I make it send post data to the browser?

我需要使用按钮而不使用表单。如何让它将发布数据发送到浏览器?

I am using:

我在用:

<button style="height: 100px; width: 300px" onClick="parent.location='form1.php'" >Fill survey</button>

回答by Xavier Barbosa

You'll need to create a form in JavaScript, then submit it. Look at this code

您需要在 JavaScript 中创建一个表单,然后提交它。看看这个代码

The HTML

HTML

<button type="button" onclick="proceed();">do</button> 

The JavaScript code

JavaScript 代码

function proceed () {
    var form = document.createElement('form');
    form.setAttribute('method', 'post');
    form.setAttribute('action', 'http://google.com');
    form.style.display = 'hidden';
    document.body.appendChild(form)
    form.submit();
}