如果选中单选按钮,则提交到其他页面


If radio button checked, then submit to different page

我有这个:

<form method="post" id="kl" action="step2.php">
<input type="radio" name="rubrik" value="bussines"></input>
<input type="radio" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>

我基本上想要的是:当选中第二个单选按钮时,将表单提交到不同的文件step2a.php。我该怎么做?Jquery,Javascript,php?

您可以使用JavaScript(绑定一个检查单选按钮值的提交侦听器,然后设置表单的操作属性)来完成这项工作,但按照以下步骤(服务器端)做一些事情会更简单、更可靠:

<form ... action="step-selector.php">

<?php
    if (isset($_POST['rubrik']) && $_POST['rubrik'] == 'bussines') {
        include('step2.php');
    } elseif (isset($_POST['rubrik']) && $_POST['rubrik'] == 'private') {
         include('step2a.php');
    } else {
         include('error-state.php');
    }
 ?>

您可以将Form修改为:

<form method="post" id="kl" action="step2.php">
<input type="radio" class="radio" rel="step2.php" name="rubrik" value="bussines"></input>
<input type="radio" class="radio" rel="step2a.php" name"rubrik" value="private"></input>
<input type="image" value="submit" src="/images/submit.png" alt="Submit" />
</form>

我为单选按钮添加了rel属性。每个都有一个url值。我还添加了一个类来使用jQuery获取元素。

现在,您将需要一些Javascript,我将使用jQuery代码:

$('.radio').click(function (){
   rad = $(this);
   radRel = rad.attr('rel');
   $('form#kl').attr('action', radRel);
});

有多种方法可以做到这一点,具体取决于您想要什么。

看看这个,它可能会帮助你达到目的;打开页面的单选按钮

您可以使用form.submit()作为onclick处理程序(而不是onchange)并更改操作。

<input type="radio" name"rubrik" value="private" onclick="this.parentNode.action='yourOtherFile.php'; this.parentNode.submit()"></input>