【问题标题】:changing form post更改表格帖子
【发布时间】:2010-10-03 12:03:19
【问题描述】:

我想在发送前更改表单发布数据。我使用一些 java 脚本代码来获取一些用户输入并希望附加这些以请求发布数据。 我想要一个将结果显示为 HTML 页面的普通帖子。 我链接 JQuery 并可以使用它的功能可能很有用。 非常感谢!

【问题讨论】:

  • 我不明白你想做什么。另外,您可能想显示一些代码
  • 当用户点击提交按钮时,表单的输入元素的值将被发送到服务器,我想在发送之前在客户端更改此数据。

标签: javascript jquery forms post


【解决方案1】:

你可以使用onSubmit事件如下图-

<form action="test.php" name="testform" onSubmit="return TestDataCheck()" >
<input type="text" value="hello" name="testinput" />
<form>

如下图定义一个函数——

<script type="text/javascript">
function TestDataCheck(){

//Here do whatever you want to do with the form or it's values.
//I am changing the value of input field in the form for your reference in same way you
//can change any elements value.

$('input[name="testinput"]').val("bye");

return true.

}
</script>

【讨论】:

  • 问题是,如何更改表单数据,我应该用什么替换评论?问题是关于我可以在 TestDataCheck 中编写的代码!
  • 供您参考,我在示例中更改了表单中输入字段的值。以同样的方式,您可以更改任何字段的值。
  • 如果您需要具体的解决方案,请发布您要替换的 html 表单和值。
  • 我明白了!我可以在不影响 UI 的情况下向表单添加一些隐藏的输入,谢谢!
【解决方案2】:

html

<form action="test.php" name="testform" id="testForm">
<input type="text" value="hello" name="testinput" />
<form>

javascript

//handle form submit
   jQuery("#testForm").submit(function(e){

      //prevent default action
      e.preventDefault; 

     //get form
     var _data=jQuery("#testForm").serialize();

     //add custom props
     _data['customProp1']='customValue1';
     _data['customProp2']='customValue2';

     //now just submit form or call ajax

    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-21
    • 2014-07-22
    • 2011-09-13
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多