表单提交按钮 onClick JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7337981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form submit button onClick JavaScript function
提问by 3mpetri
I made a simple form with radio buttons and submit button. On submit, check() function is called and it prints out text dependent on which radio button is selected.
我用单选按钮和提交按钮制作了一个简单的表单。在提交时,调用 check() 函数并根据选择的单选按钮打印文本。
But, it only works when I submit for the second time. First time I sumbit, page refreshes, ?req=on is added in the url, and that's it.
但是,它仅在我第二次提交时有效。我第一次提交时,页面刷新,在 url 中添加了 ?req=on,就是这样。
Why is that, and how can I get it to work every time I press the submit button?
为什么会这样,每次按下提交按钮时如何让它工作?
HTML:
HTML:
<form name="request" onsubmit="return check();">
<p>
<label>Pick fragments: </label>
</p>
<fieldset>
<input id="rally" type="radio" name="req" />
Longest rally
<br />
<input id="gamept" type="radio" name="req" />
Game points
<br />
<input id="setpt" type="radio" name="req" />
Set points
<br />
<br />
<input type="submit" />
</fieldset>
</form>
<ul id="uri"></ul>
And part of the script:
和脚本的一部分:
function check() {
window.location.hash = "article6";
var uri = new Array();
var vid = new Array();
$('#uri').empty();
if($('#rally')[0].checked) {
//get json with the longest rally and print it out
$.getJSON('json/longRally.json', function(json) {
for(var i = 0; i < json["results"]["bindings"].length; i++) {
uri[i] = json["results"]["bindings"][i]["longRally"].value
vid[i] = json["results"]["bindings"][i]["uriRally"].value
};
uri.sort(alphanum);
vid.sort(alphanum);
for(var i = 0; i < json["results"]["bindings"].length; i++) {
$('#uri').append('<li><a href="' + vid[i] + '">' + uri[i] + '</a></li>');
};
});
}
}
Web is located here:
网站位于:
回答by ggutenberg
Add:
添加:
return false;
To the end of your function.
到你的函数结束。