【发布时间】:2015-02-21 06:14:38
【问题描述】:
我正在使用 jQuery 向我的服务器发送请求,然后呈现结果。但是,现在我想使用一个按钮,以便仅在单击发送按钮后发送请求,而不像我之前使用无法控制的“onkeyup”的代码;因为我想在完成输入请求后而不是之前发送请求。
<script>
function Search(query_text)
{
$.get( "http://localhost:9000?query="+query_text, function( data ) {
$(\"#div2\").empty()
..............
$(\"#div2\").append("</table>")
}, "json")
.fail(function() {
$("#rep")
.append('<br/><p style="color:red">Oops! Error with XMLHttpRequest</p>')
});
;
}
</script>
这是调用 jQuery 函数的输入:
<input id="queryText" type="text" onkeyup="Search($(this).val())" /><br>
我尝试添加一个简单的按钮,然后像这样调用 jQuery 函数,但它不起作用,因为页面重新加载并且我没有得到我需要的结果:
<form class="form-inline">
<div class="form-group">
<input type="text" class="form-control" id="queryText" placeholder="Your text">
</div>
<button type="submit" class="btn btn-primary" onclick="Search($('#queryText').val())">Find courses</button>
</form>
我不知道是否有办法继续使用 GET 请求而不是使用表单并在那时发送 POST 请求。
提前谢谢你!
【问题讨论】:
-
chnnge 按钮类型提交到 type="button"。
标签: javascript jquery forms get