如果单选按钮选中,然后提交到不同的页面(If radio button checked, then

2019-07-30 11:00发布

我有这个:

<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>

我想bassicaly是:当第二个单选按钮被选中,提交表单到step2a.php,不同的文件。 我怎样才能做到这一点? jQuery的,使用Javascript,PHP的?

Answer 1:

你可以用JavaScript做到这一点(绑定提交监听器检查的单选按钮的值,然后设置表单的action属性),但它会更简单和更可靠的做线沿线的东西(服务器端):

<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');
    }
 ?>


Answer 2:

if($("input[@name=rubrik]:checked").val()=="private")
{
$("#kl").attr("action","step2a.php");
}

现在不用提交。

我没有测试代码希望它会给一个想法。



Answer 3:

你可以通过修改形式进入这样做:

<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);
});


Answer 4:

这样做有它,这取决于你想要什么的多种方式。

检查这一个,它可能会帮助你到达那里; 单选按钮打开页面



Answer 5:

您可以使用form.submit()作为的onclick处理程序(没有的onchange)和更改操作了。

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


文章来源: If radio button checked, then submit to different page