[英]How to change url based on input type selected using javaScript/Jquery?
我有一個表格
<form name="categoryform" method="post">
Category:<select name="category" id="category" >
<option value="games">games</option>
<option value="books">books</option>
<option value="toys">toys</option>
<option value="clothes">clothes</option>
</select>
Age: <select multiple name="age" id="age" >
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<input type="submit" name="submit" value="submit"/>
</form>
最初的網址是http://localhost/childzone/
如果我從“類別”中選擇了選項books
,則URL必須更改為http://localhost/childzone/books
並且當我選擇選項clothes
,URL必須是http://localhost/childzone/clothes
。
如果我還從下拉age
選擇age = 5,則該網址必須為http://localhost/childzone/clothes&age=5
。 對於選擇的多個選項,即,如果我從age
選擇5和10,則URL必須為http://localhost/childzone/clothes&age=5,10
。
我如何獲得這樣的URL。 我是php新手,任何人都可以幫忙。 先感謝您
這些用於重定向腳本而不刷新頁面。讓我知道這些是否有用
function submitnow_urlchange(){
var cat = jQuery("#category option:selected").val();
var age = jQuery("#age option:selected");
var age_holder = [];
age.each(function(){
var el = jQuery(this);
var hold = el.val();
if(el.val()) age_holder.push(hold);
});
var age_compile = age_holder.join(',');
var url = cat+"&"+age_compile;
history.pushState("", "", url);
return false;
}
這是一個更詳細的jQuery解決方案:
JS:
//Generate url for category
$("select[name='category']").change(function(e) {
e.preventDefault();
var a = "http://localhost/childzone/";
$("div.url").append(a + this.value);
});
//Generate URL for the multi-select age list
var foo = [];
var a = "http://localhost/childzone/clothes&age=";
var b = '';
$("select[name='age']").change(function(e) {
e.preventDefault();
$("select[name='age'] :selected").each(function(i, selected) {
foo[i] = $(selected).text();
if (b == '') b = b + foo[i]; //if more than one option selected, seperate it by comma.
else b = b + ',' + foo[i];
});
$("div.url").html(a + b); //Append URL to div for reference
b = ''; //Clear the string holding URL.
});
只是我們需要使用我附加在div.url
上的URL來調用div.url
。
//on books select event use
location.replace("http://localhost/childzone/books");
//on clothes select event use
location.replace("http://localhost/childzone/clothes");
不刷新:-
//on books select event use
window.history.replaceState(null, "Search Results", "localhost/childzone/books")
//on books select event use
window.history.replaceState(null, "Search Results", "localhost/childzone/books")
這是不可能的。 由於如果您嘗試更改URL,則頁面將刷新。 然后您的選擇將重置。 您可以通過添加#來執行此操作,或者必須以某種方式維護所選的值並再次進行設置。
嘗試使用此兄弟添加您的表單onsubmit =“ submitnow()”,以便該功能正常工作。讓我知道它是否存在問題
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function submitnow(){
var cat = jQuery("#category option:selected").val();
var age = jQuery("#age option:selected");
var age_holder = [];
age.each(function(){
var el = jQuery(this);
var hold = el.val();
if(el.val()) age_holder.push(hold);
});
var age_compile = age_holder.join(',');
var url = "http://localhost/childzone/?"+cat+"&"+age_compile;
return url;
}
</script>
<form name="categoryform" method="post" onsubmit="submitnow();">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.