【问题标题】:No action when submitting form using getElementbyID()使用 getElementbyID() 提交表单时无操作
【发布时间】:2015-01-26 07:57:06
【问题描述】:

我是 C++ 程序员,是 JS World 的新手。

这是指我之前的问题。

How to submit form based on certain condition?

我正在尝试根据特定条件使用 document.getElementById 提交我的表单,但没有输出。

<!doctype html>
<html lang="en">
<head>
 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<title>jQuery.post demo</title>
<body>
<meta charset="utf-8">
<div id="form-container" style="padding-top: 10px; padding-bottom: 10px;">

<script>
  var x=Math.random();;
  if(x > 0.5)
  {
      //document.writeln(x);
      document.getElementById('form-container').submit();
  }  
</script>
X Value: <input type="text" name="X"><br>
<br>
<p style="text-align: left;"><button type="button" id="submit-button">Submit</button></p>
</div>
</body>
<script>
$(document).ready(function(e) {
  $('#submit-button').click(function(event){
    $('#form-container').submit();
  )};
)};
</script>
</html>

【问题讨论】:

  • 我在您的代码中没有看到表单元素。
  • 答案是你的标签之一,你需要&lt;form&gt;

标签: javascript php jquery html forms


【解决方案1】:

您不能在&lt;div&gt; 上执行 .submit()!您需要一个带有Actionmethod 属性的&lt;form&gt; 标签。

action 属性告诉浏览器他必须将数据发送到哪里,method 属性表示以何种方式(POST 或 GET)发送数据。

<form Action="process.php" method="POST">

当然您也可以添加ID,用于识别表单。

【讨论】:

  • 但是我的项目中有一个代码部分,我在页面上有 div 元素,该元素在单击提交按钮时具有提交按钮,它使用表单中填写的数据生成一些图。
【解决方案2】:

您应该在 HTML 中使用表单并指定属性以允许表单提交:

<form id="form-container" action="url to your action where the form submit request will hit" method="get">

参考:http://www.tutorialspoint.com/html/html_form_tag.htm

【讨论】:

  • 在我的情况下,没有任何动作,但 Jquery 附加了生成绘图的按钮。
  • 一个例子就像按钮调用 Jquery 函数 $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }) .done(function( msg ) { alert("数据已保存:" + msg ); });
【解决方案3】:

你应该提交表单而不是 div 所以在表单标签中包含 url 和方法类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    相关资源
    最近更新 更多