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

0mkxixxg  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(199)

下面的代码是:

<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>

我在window.location中有一个search数据,我想将它添加到表单的提交值中。例如,url是:

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

形式的值是rnum=10,我想合并它,使之成为:

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

正如@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>

但结果仍然是这样:http://127.0.0.1:5000/result?rnum=10为10的形式。

gcuhipw9

gcuhipw91#

那么,您可以在表单中创建一个具有所需值的输入节点,然后提交它。

<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>

如果您正在寻找更新页面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>
qjp7pelc

qjp7pelc2#

我想你在这部分有语法错误

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

相关问题