1

这就是我得到的:

<form action="invoiceCreate.php" method="post">
<input type="checkbox" name="business" id="business" vaulue="yes" />

基本上,当我选中"business"复选框时,我希望表单操作更改为BusinessInoiveCreate.php而不是InvoiceCreate.php.

最好的方法是什么?

4

2 回答 2

4

由于没有指定,这里有一个不使用 jQuery的简单方法。根据浏览器的兼容性,您可能需要以不同的方式附加事件侦听器,但一般概念是相同的。

HTML

<form name="myForm" action="invoiceCreate.php" method="post">
  <input type="checkbox" name="business" id="business" vaulue="yes" />
</form>

Javascript

var form = document.getElementsByName("myForm")[0];
var checkBox = document.getElementById("business");

checkBox.onchange = function(){
  if(this.checked){
    form.action = "giveEmTheBusiness.php";
  }else{
    form.action = "invoiceCreate.php";
  }
  console.log(form.action);
};

或者类似地,将事件绑定到submit

form.onsubmit = function(){
  if(checkBox.checked)
      form.action = "giveEmTheBusiness.php"
  else
      form.action = "invoiceCreate.php";
};

例子

于 2013-04-04T15:46:00.333 回答
3
$('#business').on('change', function(){
    if ($(this).is(':checked')) {
        $('form').attr('action', 'BusinessInoiveCreate.php');
    } else {
        $('form').attr('action', 'invoiceCreate.php');
    }
});

工作演示:http: //jsfiddle.net/eV7vY/

于 2013-04-04T15:43:00.310 回答