问题描述
我有一个表格:
<form action="results.php" method="get">
<select name="genres[]">
<option value="jazz"></option>
<option value="blues"></option>
<option value="rock"></option>
</select>
</form>
提交表单,输入变得URL后 example.com/?genres%5B%5D=jazz&genres%5B%5D=blues&genres%5B%5D=rock
下一步,它看起来并不很漂亮,我在我的形式有多种输入(与约18流派),所以如果用户选择了很多,URL变得很长。我想它变成/?流派=爵士乐,蓝调,摇滚
Next to that it doesn't look very nice, I have multiple inputs in my form (and around 18 genres) so if a user selects a lot, the URL becomes very long. I'd like it to become /?genres=jazz,blues,rock
现在我读过,但它没有说明如何进行清洁的URL。下一步,我不得不使用输入作为一个数组,如 $流派= $ _GET ['风格']
在其他功能使用。
Now I've read this post, but it doesn't state how to make the cleaner URL. Next to that, I have to use the input as an array, eg $genres = $_GET['genres']
to use in another function.
推荐答案
您可以做到这一点是这样的:
you can do it like this:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function(){
$("#go").click(function(){
var g = $("#genres").val();
var href = 'results.php?genres=';
if(g != null) {
href += g;
}
document.location.href=href;
});
});
</script>
</head>
<body>
<select id="genres" size="3" multiple>
<option value="jazz">jazz</option>
<option value="blues">blues</option>
<option value="rock">rock</option>
</select>
<input type="button" value="go" name="submit" id="go">
</body>
</html>
在results.php你应该:
in results.php you should:
$g = isset($_GET['genres'])?$_GET['genres']:"";
$genres = explode(",",$g);
我有形式完全删除,因为流派现在由 document.location.href
发送。 jQuery的的 .VAL()
函数返回像预期(由崩盘,
)
i have removed the form completely because the genres are now sent by document.location.href
. The .val()
function of jquery returns the selected values like expected (imploded by ,
)
这篇关于表单输入数组$ _GET逗号分隔的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!