0

我有一个带有 2 个单选按钮选项的简单 HTML 表单,我想为每个选择的单选按钮调用不同的网页

我一直在尝试使用 javascript,但我是 javascript 新手,有没有办法通过使用 jquery 来做到这一点?

这是我的 HTML:

<form action="" id="formoid" title="" method="post">
 <div><input type="radio" name="op" id="op" value="student"><span>student</span>
 <input type="radio" name="op" id="op" value="company"><span>company</span>  
 </div>
 </div>
 <div><input type="submit" name="submit" id="submit" value="Submit"></div>
</form>

谢谢!

4

3 回答 3

1

我不确定我是否正确,但如果您的意思是更改表单的 aciton 属性,请使用以下内容:

<form action="" id="formoid" title="" method="post">
    <div><input type="radio" name="op" id="op" value="student" data-action="http://foo-bar.baz/form.php"><span>student</span>
    <input type="radio" name="op" id="op" value="company" data-action="http://foo-bar2.baz/form.asp"><span>company</span>                        </div>
    </div>
    <div><input type="submit" name="submit" id="submit" value="Submit"></div>
</form>

并在之后添加javascript代码(加载了jQuery):

<script type="text/javascript">
    $('#formoid radio[name="op"]').click( function() {
        $('#formoid').attr('action', $(this).attr('data-action') );
    } );
</script>

顺便提一句。你有无效的html结构,修复它也是个好主意:)

于 2013-04-29T19:39:07.363 回答
0
$("#submit").click(function(){
var val = $("input[type='radio']:checked").val();

if(val == "student"){
//post to 1 page
}

if(val == "company"){
//post to 1 page
}
于 2013-04-29T19:40:06.183 回答
0

javascript

<form action="#a" name="f1" title="" method="post" onSubmit=doSubmit()>
    <div><input type="radio" name="op" id="op" value="student"><span>student</span>
    <input type="radio" name="op" id="op" value="company">    <span>company</span>                        </div>
    </div>
    <div><input type="submit" name="submit" id="submit" value="Submit"></div>

    </form>

    <script>

function getRadioButtonValue(rbutton)
      {
        for (var i = 0; i < rbutton.length; ++i)
        {
          if (rbutton[i].checked)
            return rbutton[i].value;
        }
        return null;
      }


    function doSubmit(){
        val = getRadioButtonValue( document.f1.op)  

        if(val == "student"){
                document.f1.action = "student.html"
            }else{
            document.f1.action = "company.html"
        }
        document.f1.submit()
    }

    </script>
于 2013-04-29T19:47:48.550 回答