这就是我得到的:
<form action="invoiceCreate.php" method="post">
<input type="checkbox" name="business" id="business" vaulue="yes" />
基本上,当我选中"business"
复选框时,我希望表单操作更改为BusinessInoiveCreate.php
而不是InvoiceCreate.php
.
最好的方法是什么?
这就是我得到的:
<form action="invoiceCreate.php" method="post">
<input type="checkbox" name="business" id="business" vaulue="yes" />
基本上,当我选中"business"
复选框时,我希望表单操作更改为BusinessInoiveCreate.php
而不是InvoiceCreate.php
.
最好的方法是什么?
由于没有指定,这里有一个不使用 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";
};
$('#business').on('change', function(){
if ($(this).is(':checked')) {
$('form').attr('action', 'BusinessInoiveCreate.php');
} else {
$('form').attr('action', 'invoiceCreate.php');
}
});
工作演示:http: //jsfiddle.net/eV7vY/