我有一张表格
<form class="bar" action="process.php">
</form>
我的选择标记
<select id="foo">
<option value="index.php?id=1">Volvo</option>
<option value="index.php?id=2">Saab</option>
</select>
如果有人更改了所选的选项,我希望根据所选选项中的值提交表单并将页面重定向到网址
我试试
$('#foo').on('change', function(){
$('.bar').submit();
});
我发现表单已成功提交。
但是当我尝试使用以下方式重定向页面时
$('#foo').on('change', function(){
var url = $(this).val();
$('.bar').submit();
if(url){
window.location = url;
}
});
页面已成功重定向,但表单未提交 注意:我不打算使用ajax
答案 0 :(得分:1)
1st:使用$("#foo")
代替$(#foo)
第二名:在select
更改活动时..使用action
将默认表单$('.bar').attr('action' , url)
网址替换为所选值网址,然后使用.submit()
参见下一个例子
$(document).ready(function(){
$("#foo").on('change', function(){
var url = $(this).val();
console.log(url.replace('index','process'));
$('.bar').attr('action' , url.replace('index','proccess')).submit();
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="bar" action="process.php">
<input type="submit" value="submit"/>
</form>
<select id="foo">
<option value="index.php?id=1">Volvo</option>
<option value="index.php?id=2">Saab</option>
</select>
&#13;
你可以用process.php?id=
替换你从值获得的id的动作网址。然后在process.php中你可以使用
if($_GET['id'] == '1'){
exit(header("location: index.php?id=1"));
}
if($_GET['id'] == '2'){
exit(header("location: index.php?id=2"));
}
要重定向,不使用if语句
exit(header("location: index.php?id=".$_GET['id']));
注意:如果exit(header("location: index.php?id=
未重定向到index.php
尝试使用/index.php?id
,./index.php?id
,&#39; ../ index ?.PHP ID&#39; ,或者使用完整的网址http://www.yourwebsite.com/index.php?id