我在wordpress页面上创建了一个动态选择框来选择类别和子类别。
输出网址应为http://myurl.com/category/maincategory/subcategory 但表格提供了http://myurl.com/category/maincategory/?xy=maincategory+subcategory。
我认为最简单的解决方案是,如果jQuery脚本将删除“?”之间的特定部分和实时网址中的“+”,但我不知道如何。
这将是一个旅馆发现者,这里是完整的代码。一些脚本用于动态选择框宽度。
<form method="get" action="<?php bloginfo('url'); ?>">
<fieldset>
I'd like to find a nice hostel in
<select name="category_name" id="orszag" style="width: auto;">
<option value="hostels">any country</option>
<?php
// generate list of categories
$categories = get_categories('hide_empty=1&style=none&parent=2');
foreach ($categories as $category) {
echo '<option value="', $category->slug, '">', $category->name, "</option>\n";
}
?>
</select>
,
<select class="calc_dropdown calclinkone" id="varos" name="varos" style="width: auto;">
<option value="hostels ">any city</option>
<option value="hungary ">any city</option>
<?php
// generate list of categories
$categories = get_categories('child_of=3&hide_empty=0');
foreach ($categories as $category) {
echo '<option value="hungary ', $category->slug, '">', $category->name, "</option>\n";
}
?>
<option value="uk ">any city</option>
<?php
// generate list of categories
$categories = get_categories('child_of=6&hide_empty=0');
foreach ($categories as $category) {
echo '<option value="uk ', $category->slug, '">', $category->name, "</option>\n";
}
?>
<option value="slovakia ">any city</option>
<?php
// generate list of categories
$categories = get_categories('child_of=17&hide_empty=0');
foreach ($categories as $category) {
echo '<option value="slovakia ', $category->slug, '">', $category->name, "</option>\n";
}
?>
<option value="germany ">any city</option>
<?php
// generate list of categories
$categories = get_categories('child_of=4&hide_empty=0');
foreach ($categories as $category) {
echo '<option value="germany ', $category->slug, '">', $category->name, "</option>\n";
}
?>
</select>
<br>
for
<select>
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
<option value="">4</option>
<option value="">5</option>
<option value="">6</option>
</select>
person for
<select>
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
<option value="">4</option>
<option value="">5</option>
<option value="">6</option>
</select> nights.<br>
<button type="submit"><i class="fa fa-search"></i> Search</button>
</fieldset>
</form>
<script>
$("#orszag").change(function() {
if($(this).data('options') == undefined){
$(this).data('options',$('#varos option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value*=' + id + ']');
$('#varos').html(options);
});
$('select').change(function() {
var $opt = $(this).find("option:selected");
var $span = $('<span>').addClass('tester').text($opt.text());
$span.css({
'font-family': $opt.css('font-family'),
'font-style': $opt.css('font-style'),
'font-weight': $opt.css('font-weight'),
'font-size': $opt.css('font-size')
});
$('body').append($span);
// The 30px is for select open icon - it may vary a little per-browser
$(this).width($span.width() + 10);
$span.remove();
});
$('select').change();
</script>
谢谢!
答案 0 :(得分:1)
使用JQuery的替换方法
var mystring = "hellothis";
var m = mystring.replace("this", "that");
alert(m);
会产生“hellothat”
var myUrl = "http://myurl.com/category/maincategory/?xy=maincategory+subcategory"
var newURL = myUrl.replace("?xy=maincategory+","");
应该给你你想要的东西
答案 1 :(得分:0)
我现在在这。 我有这个
$(document).ready(function () {
if(window.location.href.indexOf("?") > -1) {
location.href=location.href.replace("?varos=germany+", "");
}
});
如果我在德国,代码运作良好。现在我必须将?varos=germany+
部分更改为定义字符串之间动态部分的内容?和+并删除它。