Change value of input and submit form in JavaScript

前端 未结 8 1360
死守一世寂寞
死守一世寂寞 2020-11-30 03:20

I\'m currently working on a basic form. When you hit the submit button, it should first change the value of a field, and then submit the form as usual. It all looks a bit li

相关标签:
8条回答
  • 2020-11-30 03:21

    This might help you.

    Your HTML

    <form id="myform" action="action.php">
    <input type="hidden" name="myinput" value="0" />
    <input type="text" name="message" value="" />
    <input type="submit" name="submit" onclick="save()" />
    </form>
    

    Your Script

        <script>
            function save(){
                $('#myinput').val('1');
                $('#form').submit();
            }
        </script>
    
    0 讨论(0)
  • 2020-11-30 03:23

    You could do something like this instead:

    <form name="myform" action="action.php" onsubmit="DoSubmit();">
        <input type="hidden" name="myinput" value="0" />
        <input type="text" name="message" value="" />
        <input type="submit" name="submit" />
    </form>
    

    And then modify your DoSubmit function to just return true, indicating that "it's OK, now you can submit the form" to the browser:

    function DoSubmit(){
      document.myform.myinput.value = '1';
      return true;
    }
    

    I'd also be wary of using onclick events on a submit button; the order of events isn't immediately obvious, and your callback won't get called if the user submits by, for example, hitting return in a textbox.

    0 讨论(0)
  • 2020-11-30 03:25

    No. When your input type is submit, you should have an onsubmit event declared in the markup and then do the changes you want. Meaning, have an onsubmit defined in your form tag.

    Otherwise change the input type to a button and then define an onclick event for that button.

    0 讨论(0)
  • 2020-11-30 03:31

    You can use the onchange event:

    <form name="myform" id="myform" action="action.php">
        <input type="hidden" name="myinput" value="0" onchange="this.form.submit()"/>
        <input type="text" name="message" value="" />
        <input type="submit" name="submit" onclick="DoSubmit()" />
    </form>
    
    0 讨论(0)
  • 2020-11-30 03:33

    You're trying to access an element based on the name attribute which works for postbacks to the server, but JavaScript responds to the id attribute. Add an id with the same value as name and all should work fine.

    <form name="myform" id="myform" action="action.php">
      <input type="hidden" name="myinput" id="myinput" value="0" />
      <input type="text" name="message" id="message" value="" />
      <input type="submit" name="submit" id="submit" onclick="DoSubmit()" />
    </form>
    
    function DoSubmit(){
      document.getElementById("myinput").value = '1';
      return true;
    }
    
    0 讨论(0)
  • 2020-11-30 03:35

    Here is simple code. You must set an id for your input. Here call it 'myInput':

    var myform = document.getElementById('myform');
    myform.onsubmit = function(){
        document.getElementById('myInput').value = '1';
        myform.submit();
    };
    
    0 讨论(0)
提交回复
热议问题