jQuery -提交表单到RESTful URL
jQuery - Submit Form to RESTful URL
我脑子里一片空白,所以需要一些指导。
<form action="/" id="searchForm">
<input type="text" name="s" placeholder="Search...">
<input type="submit" value="Search">
</form>
我需要这个表单,所以页面重定向到一个URL,比如
http://example.com/rest/ful/url/{value of search}/
,例如http://example.com/rest/ful/url/jQuery/
,如果我搜索jQuery
我甚至需要jQuery吗?
使用jquery解决方案:
$('#searchForm').on('submit', function(event){
$('[name=s]').prop('disabled', true)
$(this).attr('action', 'http://example.com/rest/ful/url/' + $('[name=s]').val());
});
https://jsfiddle.net/3y4efht0/1/使用jquery的.submit
事件获取输入值,使用window.location.href
来实现您的需求
请检查下面的代码片段。
$("#searchForm").submit(function( event ) {
var searchTerm = $("input[name='s']").val();
if($.trim(searchTerm)!=""){
var redirectURL = "http://example.com/rest/ful/url/"+$.trim(searchTerm)+"/";
console.log(redirectURL);
window.location.href=redirectURL;
}else{
alert("Please enter search term!");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/" id="searchForm">
<input type="text" name="s" placeholder="Search...">
<input type="submit" value="Search">
</form>
你不需要jquery来做这件事,但它可以帮助你,你必须监听提交事件,防止默认行为并重定向到你想要的页面:
$('#searchForm').on('submit', function(e){
e.preventDefault();
document.location.href = 'http://example.com/rest/ful/url/'+$('#s').val()+'/'
})
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- 使用php或javascript从facebook相册URL中删除多余的部分
- Angular JS IE9 Hashbang url rewriting
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 直接下载文件,而不是从window.open(url)
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 通过js在新选项卡中有条件地打开url
- CKFinder 3为所选文件返回错误的URL
- 如何获取不属于我项目的上一页的URL
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 在URL中传递JS对象
- 将纯文本URL转换为可单击链接
- Javascript html每点击一次就会更改url
- 以角度管理动态URL
- Mathias URL shortener
- 主干模型如何知道要使用哪个 RESTful URL
- 如何将RESTful API映射到BackboneJS模型和集合中的URL
- jQuery -提交表单到RESTful URL
- POST on JAVA Restful WebService URL from JavaScript
- Backbone中用于RESTful交互的集合的URL