2013-04-04 49 views
0

何时改变形式的行动这是我得到:PHP/JavaScript的复选框选中

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

基本上当我检查"business"复选框,我希望表单行动,改变对BusinessInoiveCreate.php而不是InvoiceCreate.php

这样做的最佳方式是什么?

+1

使用javascript。你尝试过什么吗? – aleation 2013-04-04 15:39:31

+1

不这样做。改为使逻辑分支决策服务器端。 – Quentin 2013-04-04 15:43:07

回答

2
$('#business').on('change', function(){ 
    if ($(this).is(':checked')) { 
     $('form').attr('action', 'BusinessInoiveCreate.php'); 
    } else { 
     $('form').attr('action', 'invoiceCreate.php'); 
    } 
}); 

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

+0

谢谢你chrx,完美的作品! – 2013-04-04 15:48:36

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

EXAMPLE