在按下提交按钮后更改表单值 [英] Change form values after submit button pressed

查看:90
本文介绍了在按下提交按钮后更改表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量的挖掘,我发现问题是我如何将CKEditor集成到我的页面。

After a lot of digging around, I found out that the problem was in how I integrated the CKEditor into my page. The simple and obvious way does work in this case, as laid out in the accepted answer.

您好,

我需要在按下提交按钮后,但在实际提交之前更改表单的值。

I need to change the values of a form, after the submit button has been pressed, but before the actual submission has taken place.

我尝试钩入表单的提交事件,并手动更改文本字段值,但它看起来像实际上不会改变提交的值。

I've tried hooking into the "submit" event of the form, and changing the text field values there manually, but it looks like that doesn't actually change the values submitted.

任何想法?

推荐答案

我很好奇你的陈述,提交处理程序没有为您工作。它为我。我使用它多年来填写隐藏的字段,然后发送表单;应该也适用于其他表单字段。

I'm curious about your statement that the submit handler didn't work for you. It does for me. I've used it for years to fill in hidden fields before sending forms in; should work for other form fields as well.

示例( live copy ) ):

HTML:

<form id='theForm'
    action='http://www.google.com/search'
    method='GET' target='_new'>
      <label>Search for:
        <input type='text' name='q' id='txtSearch'></label>
      <input type='submit' id='btnSearch' value='Search'>

JavaScript:

JavaScript:

window.onload = function() {

  document.getElementById('theForm').onsubmit = function() {
    var txt = document.getElementById('txtSearch');
    txt.value = "updated " + txt.value;
  };
};​

在Windows和Chrome,Firefox上测试并使用IE6和IE7 ,和Linux上的Opera。

Tested and working on IE6 and IE7 on Windows, and Chrome, Firefox, and Opera on Linux.

更新:根据您在下面的评论,使用jQuery。使用jQuery的一切也很好:

Update: Based on your comment below, you're using jQuery. Works fine using jQuery for everything as well:

$('#theForm').submit(function() {
  var txt = $('#txtSearch');
  txt.val("updated " + txt.val());
});

实例测试并在同一组浏览器上工作。 此版本使用更开放的搜索,而不是 id ,还是工作。

Live example Tested and working on the same set of browsers. This version uses a more open search rather than an id, and also still works.

这篇关于在按下提交按钮后更改表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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