简体   繁体   English

如何使用 JS 向提交的表单添加值

[英]How to add a value to submitted form using JS

I have the following html code:我有以下 html 代码:

<form>
      <input autofocus type="text" name="rnum" id="rnum" class="input-field" placeholder="Number of rows in a page">
</form>
<script type="text/javascript">
        document.getElementById('rnum')
        .addEventListener('keyup', function(event) {
               if (event.code === 'Enter') {
                  .append(window.location.search)
                  .toString();
                  event.preventDefault();
                  document.querySelector('form').submit();
               }
        });</script>

I have a search data in window.location that I want to add to the submitted value of the form.我在window.location中有一个search数据,我想将其添加到表单的提交值中。 For instance, the url is:例如,url 是:

http://127.0.0.1:5000/result?searchbox=cor

and the value of the form is rnum=10 that I want to combine and make:并且表单的值是rnum=10我想组合并制作:

http://127.0.0.1:5000/result?searchbox=cor&rnum=10

update 1: As @Yasir suggested, I replaced the code,更新1:正如@Yasir 建议的那样,我替换了代码,

<form style="float: right;">
 <input autofocus type="text" name="rnum" id="rnum" class="input-field" placeholder="Number of rows in a page">
</form>
<script type="text/javascript">
     document.getElementById('rnum')
     .addEventListener('keyup', function(event) {
     if (event.code === 'Enter') {
         let child = document.createElement('input');
         child.name = "searchBox";
         child.value = window.location.search.toString();
         event.preventDefault();
         var form = document.querySelector('form');
             form.appendChild(child);
             form.submit()
      }
});</script>

But still the result is like: http://127.0.0.1:5000/result?rnum=10 for 10 in form.但结果仍然是这样的: http://127.0.0.1:5000/result?rnum=10 for 10 in form。

There's a couple possible solutions here and here , you could go there to figure out the solution you need. 这里这里有几个可能的解决方案,您可以在 go 那里找出您需要的解决方案。

well, you can create an input node with the desired value within the form and then submit it.好吧,您可以在表单中创建一个具有所需值的输入节点,然后提交它。

<form>
      <input autofocus type="text" name="rnum" id="rnum" class="input-field" placeholder="Number of rows in a page">
</form>
<script type="text/javascript">
        document.getElementById('rnum')
        .addEventListener('keyup', function(event) {
               if (event.code === 'Enter') {
                  let child = document.createElement('input');
                  child.name = "searchBox";
                  child.value = window.location.search.toString();
                  event.preventDefault();
                  var form = document.querySelector('form');
                  form.appendChild(child);
                  form.submit()
               }
        });</script>

and if you are looking for updating the page url appending some value from form input如果您正在寻找更新页面 url 从表单输入中附加一些值

<script type="text/javascript">
        document.getElementById('rnum')
        .addEventListener('keyup', function(event) {
               event.preventDefault();
               if (event.code === 'Enter') {
                  let input = document.getElementById("rnum");
                  window.location.search += "&rnum=" +input.value;
               }
        });</script>

I think you have a syntax error in this part我认为你在这部分有语法错误

if (event.code === 'Enter') {
 .append(window.location.search) 
                  .toString();// this is an error since we are not appending the string anywhere
                  event.preventDefault();
                  document.querySelector('form').submit();
               }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM