Change form values after submit button pressed

前端 未结 3 925
长发绾君心
长发绾君心 2020-11-27 07:21

[EDIT] 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 c

3条回答
  •  清酒与你
    2020-11-27 07:24

    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.

    Example (live copy):

    HTML:

    JavaScript:

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

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


    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());
    });
    

    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.

提交回复
热议问题