简体   繁体   English

html onsubmit无法启动Javascript函数

[英]html onsubmit doesn't start Javascript function

I have a form with a Javascript populated collection of cacheable items, and a text box that is an id that is entered by a user. 我有一个表单,其中包含Javascript填充的可缓存项的集合,以及一个文本框,它是用户输入的ID。 I want on the form submit to redirect the user to the JSON response from the rest call. 我想在表单提交中将用户重定向到其余调用的JSON响应。 The rest URL works when I enter directly into the browser however transferring that to html and js is beating me. 当我直接进入浏览器时,其余的URL起作用,但是将其传输到html和js却使我不胜其烦。 The form on submit does nothing when the submit is clicked. 单击提交时,提交表单不执行任何操作。

My form - 我的表格-

<form id="query" action="get" onsubmit="return restLink();">
  <td>
    <select id="selectCacheableItemType">
      <option>Select a Cacheable Item Type</option>
    </select>
  </td>
  <td>
    Cacheable item Id: <input type="text" id="cacheableId"><br>
  </td>
</form>

The cacheable item type is populated here - 可缓存项类型在此处填充-

window.onload = function cacheItemType(){
    var select = document.getElementById("selectCacheableItemType");
    var options = ["1", "2", "3", "4", "5", "6","7"];
    for(var i = 0; i < options.length; i++) {
        var opt = options[i];
        var el = document.createElement("option");
        el.textContent = opt;
        el.value = opt;
        select.appendChild(el);
    }
};

and the restLink() function is - 而restLink()函数是-

function restLink() {   
    cachetypename = document.getElementyId('selectCacheableItemType').getValue();
    cacheid = document.getElementyId('cacheableId').getValue();
    return window.location.href = "http://localhost:8080/rest/query/"+ cachetypename + "/" + cacheid;
}

Any help is much appreciated. 任何帮助深表感谢。

You are trying to set window.location.href. 您正在尝试设置window.location.href。 You either want to submit the form to this url. 您要么想要将表单提交到该URL。 Or you don't need a form at all and just want to change the location to this url. 或者,您根本不需要表单,只想将位置更改为此URL。 You are combining 2 things and that is why it isn't working. 您正在组合两件事,这就是为什么它不起作用的原因。 I would get rid of the from and just create a button with onclick calling your restLink() method. 我将摆脱它,仅使用onclick创建一个按钮来调用restLink()方法。 No need for a form here. 这里不需要表格。

Something like this: 像这样:

<table>
<tr>
  <td>
    <select id="selectCacheableItemType">
      <option>Select a Cacheable Item Type</option>
    </select>
  </td>
  <td>
    Cacheable item Id: <input type="text" id="cacheableId"><br>
  </td>
</tr>
<tr>
    <input type="button" id="enterButton" onclick="restLink()">
</tr>
</table>

Have you tried adding a submit button? 您是否尝试过添加提交按钮?

<form id="query" action="get" onsubmit="return restLink();">
  <td>
    <select id="selectCacheableItemType">
      <option>Select a Cacheable Item Type</option>
    </select>
  </td>
  <td>
    Cacheable item Id: <input type="text" id="cacheableId"><br>
  </td>
 <input type="submit" value="Submit" id="submitForm"/>
</form>

And then: 接着:

$(document).ready(
function(){
    cacheItemType();
 var select = $("#selectCacheableItemType");
    var options = ["1", "2", "3", "4", "5", "6","7"];
    for(var i = 0; i < options.length; i++) {
        var opt = options[i];
        var el = document.createElement("option");
        el.textContent = opt;
        el.value = opt;
        select.appendChild(el);
});

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

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