2014-10-28 90 views
0

我有一个表单,其中包含可缓存项目的Javascript填充集合,以及由用户输入的ID为文本框。我希望表单提交将用户重定向到来自其他调用的JSON响应。当我直接进入浏览器时,其余的URL起作用,但是将它转移到html和js会击败我。点击提交时,提交表单不会执行任何操作。html onsubmit无法启动Javascript功能

我的形式 -

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

的缓存项目类型在这里居住 -

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

和restLink()函数 -

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

任何帮助深表感谢。

+1

哪里提交按钮?您的HTML无效。你不能在td标签周围有表格。 – epascarello 2014-10-28 14:27:58

+0

您是否尝试添加提交输入?并在该提交输入上进行onclick事件? – guramidev 2014-10-28 14:28:35

+0

设置'window.location.href'会让你转到那个页面。其次,当你的JavaScript中有错误时,你的函数将不会被调用/ finnished。在浏览器中打开您的开发人员工具 - >控制台,查看您提交时是否返回错误。 – 2014-10-28 14:30:40

回答

2

您正试图设置window.location.href。你要么提交表单到这个URL。或者你根本不需要表单,只是想将位置更改为此网址。你正在合并2件事,这就是为什么它不工作。我会摆脱从,并创建一个按钮与onclick调用你的restLink()方法。这里不需要表格。

事情是这样的:

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

这是最适合我的答案。非常感谢。有投票并接受剔。 – 2014-10-28 16:24:41

+0

欢迎您的光临,我可以帮助您! – brso05 2014-10-28 16:38:17

0

你有没有尝试添加一个提交按钮?

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

然后:

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