Javascript 处理URL重定向和表单提交()的onchange事件

Javascript 处理URL重定向和表单提交()的onchange事件,javascript,jquery,Javascript,Jquery,我有表格提交,因为它是一个简单的 onchange="submit();" <div class="cities">Select City <form method="post" action="<?php echo $PHP_SELF;?>" style="float:right;"><select name="city_select" id="city_select" onchange="su

我有表格提交,因为它是一个简单的

onchange="submit();"

                            <div class="cities">Select City <form method="post" action="<?php echo $PHP_SELF;?>" style="float:right;"><select name="city_select" id="city_select" onchange="submit();"><option>-----</option>
                        <option value="Beijing" <?php if($_SESSION['city_selected'] == 'Beijing') { echo 'selected=selected';} else { echo ''; }?> >Beijing</option>
                        <option value="Shanghai" <?php if($_SESSION['city_selected'] == 'Shanghai') { echo 'selected=selected';} else { echo ''; }?> >Shanghai</option>
                        <option value="Guangzhou" <?php if($_SESSION['city_selected'] == 'Guangzhou') { echo 'selected=selected';} else { echo ''; }?> >Guangzhou</option>
                        <option value="Manila" <?php if($_SESSION['city_selected'] == 'Manila') { echo 'selected=selected';} else { echo ''; }?> >Manila</option>
                        <option value="HongKong" <?php if($_SESSION['city_selected'] == 'HongKong') { echo 'selected=selected';} else { echo ''; }?> >Hong Kong</option>
                        <option value="Tokyo" <?php if($_SESSION['city_selected'] == 'Tokyo') { echo 'selected=selected';} else { echo ''; }?> >Tokyo</option>
                        <option value="Seoul" <?php if($_SESSION['city_selected'] == 'Seoul') { echo 'selected=selected';} else { echo ''; }?> >Seoul</option>
                        <option value="Taipai" <?php if($_SESSION['city_selected'] == 'Taipai') { echo 'selected=selected';} else { echo ''; }?> >Taipai</option>
                        </select></form>
                        <div style="clear:both;"></div>
                        </div>
我正在尝试提交表单并重定向到url。我想知道这是否可能。我尝试实现jquery$.change事件,但在表单提交之前没有触发


如果有人能帮忙,那就太好了。谢谢

您的提交功能在哪里

你能这样试试吗

<select name="city_select" id="city_select" onchange="this.form.submit();">

它应该可以工作

您还没有显示submit函数的功能,但是如果它执行的是普通表单提交而不是AJAX,那么您需要在以后重定向

通常,您会让表单正常提交,然后当下一个页面加载时,该页面会设置一个“位置”标题,告诉浏览器重定向

例如,在表单提交时运行的任何PHP代码中,都需要执行以下操作来重定向:

header('Location', 'http://mysite.com/page-to-redirect-to.php');

您只需要Jquery表单插件

使用此插件,表单将提交到“操作”地址,而不离开页面。完成后,可以使用回调函数重定向

<script>
    $(document).ready(function(){
        $('#FORM_ID').ajaxForm(function(data) {
            window.location="http://www.REDIRECT_URL.com/"; 
        });                          
    });
</script>
如果您需要此更改,您可以使用此更改:

<script>
    $(document).ready(function(){   

        $('#FORM_ID').ajaxForm(function(data) {
            window.location="http://www.REDIRECT_URL.com/"; 
        });  

        $('#city_select').change(function(){
            $('#FORM_ID').trigger('submit');
        });

    });
</script>
只需通过以下方式在php中实现: 标题“位置:”。_url_至_重定向

}


这对我很有用:,干杯。

onchange=submit;工作很好,这不是问题。适用于单个表单提交表单ID,但正如您所看到的,我需要一些动态的东西。例如,OPT_SEL1、OPT_SEL2等。不仅仅是一次提交。你能给我一个可能的例子和示例代码吗?在发送任何输出之前,必须调用DynamicRemember头,您需要执行人员操作,然后调用此函数。action=应该解释它的作用。它使用选项包含的值提交表单
if(isset($_POST['city_select'])) {

$_SESSION['city_selected'] = $_POST['city_select'];

if($_POST['city_select'] == "Beijing") {
    header("Location: /city-beijing/");
}
if($_POST['city_select'] == "Shanghai") {
    header("Location: /city-shanghai/");
}
if($_POST['city_select'] == "Guangzhou") {
    header("Location: /city-guangzhou/");
}
if($_POST['city_select'] == "Manila") {
    header("Location: /city-manila/");
}
if($_POST['city_select'] == "HongKong") {
    header("Location: /city-hong-kong/");
}
if($_POST['city_select'] == "Tokyo") {
    header("Location: /city-tokyo/");
}
if($_POST['city_select'] == "Seoul") {
    header("Location: /city-seoul/");
}
if($_POST['city_select'] == "Taipai") {
    header("Location: /city-taipai/");
}

else{

}