Adding value to hidden input tag and submitting to form

I have a test page that displays my POST data. I am trying to view the post that I just submitted but I am unable to see the added payload I added in my form.

HTML:

  <form id="test-form" action="/test/" method="post"> {# pass data to /test/ URL #}
    <div class="test-button-set">
      <button type="button" id="hdfs-test" class="btn btn-default btn-lg selected">HDFS</button>
      <button type="button" id="hive-test" class="btn btn-default btn-lg">HIVE</button>
      <button type="button" id="hdfs-hive-test" class="btn btn-default btn-lg">BOTH</button>
    </div>
    <button id="submit-test" type="submit" class="btn btn-default btn-lg">Submit</button>
    <input type="hidden" id="payload">
  </form>

JS:

var testData = {
        "timestamp": "",
        "hive": 0,
        "hdfs": 0,
        "beacons":[]
    };

var testRun = document.getElementById("test-form");
    testRun.addEventListener('submit', function(event) {
        event.preventDefault();
        testData["timestamp"] = new Date().getTime();
        console.log(testData);
        var payload = document.getElementById("payload");
        payload.value = testData;
        $(this).trigger('submit', true);
    });

Currently, I cannot see the value I gave to <input type="hidden" id="payload"> which makes me to believe that it is not getting posted.


You need to give the input a name:

<input type="hidden" id="payload" name="payload">

See this SO answer: Difference between id and name attributes in HTML


You need to add a name attribute to your hidden tag.

The name is what you refer to it by.

<input type="hidden" name="payload" id="payload">
链接地址: http://www.djcxy.com/p/88062.html

上一篇: 我可以将自定义属性添加到HTML标记吗?

下一篇: 为隐藏的输入标签添加值并提交表单