admin管理员组文章数量:1393063
I'm currently using ajax and jquery to update search results with a form that has different categories for filtering.
My problem is that I want to be able to pass different variables to the url I'm using for my ajax from checkboxes and other form elements from the current page.
I have the url of "/?cat=tech&min=5&max=30" in my ajax call below and I need that url to update to something like "/?cat=service&min=10&max=30" or even remove a param if it's not even selected like "/?min=5"
<form>
<input type="radio" name="cat" value="" /> None<br />
<input type="radio" name="cat" value="service" /> Service<br />
<input type="radio" name="cat" value="tech" /> Tech<br />
<input type="radio" name="cat" value="other" /> Other<br />
<input type="radio" name="min" value="5" /> 5<br />
<input type="radio" name="min" value="10" /> 10<br />
<input type="radio" name="min" value="15" /> 15<br />
<input type="radio" name="max" value="5" /> 5<br />
<input type="radio" name="max" value="10" /> 10<br />
<input type="radio" name="max" value="15" /> 15<br />
</form>
<div class="result"></div>
<script type="text/javascript">
$(document).ready(function(){
$('.filter').live('click focus', function(){
$.ajax({
url: /?cat=tech&?min=5&max=30,
context: document.body,
success: function(data) {
$('.result').html(data);
}
});
});
});
</script>
Some how the url in the ajax call above needs to have the params update in the url. Also, not every param will always be included or needed.
It would be nice if I could use something like PHP's http_build_query function that would automatically know what values of the array to replace or something like that but I'm really stuck as to what to do. I'm not sure if I need to create an array and concatantate
Ideally I would be able to have something like "" if that's at all doable.
I'm currently using ajax and jquery to update search results with a form that has different categories for filtering.
My problem is that I want to be able to pass different variables to the url I'm using for my ajax from checkboxes and other form elements from the current page.
I have the url of "http://example./search/results/?cat=tech&min=5&max=30" in my ajax call below and I need that url to update to something like "http://example./search/results/?cat=service&min=10&max=30" or even remove a param if it's not even selected like "http://example./search/results/?min=5"
<form>
<input type="radio" name="cat" value="" /> None<br />
<input type="radio" name="cat" value="service" /> Service<br />
<input type="radio" name="cat" value="tech" /> Tech<br />
<input type="radio" name="cat" value="other" /> Other<br />
<input type="radio" name="min" value="5" /> 5<br />
<input type="radio" name="min" value="10" /> 10<br />
<input type="radio" name="min" value="15" /> 15<br />
<input type="radio" name="max" value="5" /> 5<br />
<input type="radio" name="max" value="10" /> 10<br />
<input type="radio" name="max" value="15" /> 15<br />
</form>
<div class="result"></div>
<script type="text/javascript">
$(document).ready(function(){
$('.filter').live('click focus', function(){
$.ajax({
url: http://example./search/results/?cat=tech&?min=5&max=30,
context: document.body,
success: function(data) {
$('.result').html(data);
}
});
});
});
</script>
Some how the url in the ajax call above needs to have the params update in the url. Also, not every param will always be included or needed.
It would be nice if I could use something like PHP's http_build_query function that would automatically know what values of the array to replace or something like that but I'm really stuck as to what to do. I'm not sure if I need to create an array and concatantate
Ideally I would be able to have something like "http://example./search/results/cat/tech/min/5/30" if that's at all doable.
Share Improve this question asked May 31, 2011 at 0:13 bigmike7801bigmike7801 3,9689 gold badges52 silver badges79 bronze badges4 Answers
Reset to default 2url: 'http://example./search/results/?' + $('form').serialize(),
or
url: 'http://example./search/results/?' + $('form').serialize().replace(/&=/, '/'),
Use jQuery's serialize()
method.
jsFiddle.
Well, what you're really trying to do is submit the form without refreshing the page. So here's how to do that:
- Add a submit button to the form
- Hook into the submit event of the form
- Cancel the default action which is to refresh the page with the response
- Submit via ajax and handle the response
Like this:
<form id="MyFilterForm" action="/myBaseUrl" method="POST">
...
<input type="submit" value="Filter"/>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('#MyFilterForm').submit(function(event){
// cancel the default action
event.preventDefault();
var theForm = $(this);
$.post(theForm.attr('action'), theForm.serialize(), function (data){
$('.result').html(data);
});
});
});
</script>
You can check which radio inputs have been selected and build the url accordingly e.g:
<input id="radCat" type="radio" name="cat" value="service" /> Service<br />
string url = "http://example./search/results/?";
if($("#radCat").attr("checked").val() == "checked") {
url = url + "cat=" + $("#radCat").attr("value").val();
}
本文标签: phpHow do I construct query strings for ajax url with jquery or javascriptStack Overflow
版权声明:本文标题:php - How do I construct query strings for ajax url with jquery or javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744604049a2615244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论