我有一个问题。
我已经使用jQuery验证创建了一个表单输入,我使用Ajax提交它
使用一个提交处理程序我没有问题,但是如何使用两个按钮呢?
按钮
<table border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td align="center">
<input type="submit" value="SAVE CONTINUE" name="SC" id='SC' class="ui-input-btn ui-btn ui-shadow" data-role="button">
</td>
<td align="center">
<input type="submit" value="SAVE FINISH" name="SF" id='SF' class="ui-input-btn ui-btn ui-shadow" data-role="button">
</td>
</table>
和行动
$(document).ready(function(){
checkConnection();
$("#FORMX").validate({
//errorClass:'myClass',
rules: {
PRODUK: {
required: true
//NOSYMBOL: false
},
SHIFT:{
required: true
}
},
messages: {
PRODUK: {
required: "Barang belum terisi"
},
SHIFT:{
required: "Shift belum dipilih"
}
},
submitHandler: function(form) {
$.ajax({
type:"POST",
url: "adonan_action.php",
dataType: "json",
data: $("#FORMX").serialize() + "&KOPLO=tipe",
success: function(data){
//alert(data);
//if($.trim(php_script_response) == "sukses"){
if(data.status == "sukses"){
alert('Input Berhasil');
//window.location='logout.php';
}
else if(data.status == 'gagal'){
alert("Error on query!");
}
},
});//end ajax
}
});//end validate
});//end ready function
如何处理按钮id="SF"
?
我试过这样的事情并没有用。
submitHandler: function(form) {
$("#SC").on("click", function(){
var tipe = "SC";
});
$("#SF").on("click", function(){
var tipe = "SF";
});
$.ajax({
type:"POST",
url: "adonan_action.php",
dataType: "json",
data: $("#FORMX").serialize() + "&KOPLO=tipe",
success: function(data){
//alert(data);
//if($.trim(php_script_response) == "sukses"){
if(data.status == "sukses"){
alert('Input Berhasil');
//window.location='logout.php';
}
else if(data.status == 'gagal'){
alert("Error on query!");
}
},
});//end ajax
我只需要从按钮中捕获不同的id
或值,然后我将其与ajax一起发送到另一个页面。
任何人都可以帮忙吗?
答案 0 :(得分:1)
我已经解决了,就像这样
$("#SC").on("click", function(){
//var BTN = "SC";
if($("#FORMX").valid()){
$.ajax({
type:"POST",
url: "produksi_action.php",
dataType: "json",
data: $("#FORMX").serialize() + "&KOPLO=SC",
success: function(data){
alert(data);
//if($.trim(php_script_response) == "sukses"){
if(data.status == "sukses"){
alert('Input Berhasil');
}
else if(data.status == 'gagal'){
alert("Error on query!");
}
},error:function(dt){
alert("error");
}
});//end ajax
}
else{
alert('Tolong Lengkapi Kolom yang Kosong..!');
}
});//end click
$("#SF").on("click", function(){
//var BTN = "SC";
if($("#FORMX").valid()){
$.ajax({
type:"POST",
url: "produksi_action.php",
dataType: "json",
data: $("#FORMX").serialize() + "&KOPLO=SF",
success: function(data){
alert(data);
//if($.trim(php_script_response) == "sukses"){
if(data.status == "sukses"){
alert('Input Berhasil');
}
else if(data.status == 'gagal'){
alert("Error on query!");
}
},error:function(dt){
alert("error");
}
});//end ajax
}
else{
alert('Tolong Lengkapi Kolom yang Kosong..!');
}
});//end click
无需提交处理程序。
感谢
答案 1 :(得分:0)
从以下代码中,您可以找到单击的按钮。这是你想要的吗? 1.创建一个全局变量,在下面的代码中它是btnClicked 2.在html标记中,添加onlclick,以便在btn被点击时更改btnClicked 3.在Ur提交处理程序
中访问此btnClicked变量参考下面的代码!!!!
submitHandler: function(form) {
alert(btnClicked); //Here in thi variable you can find the id of the btn clicked :)
$.ajax({
type: "POST",
url: "adonan_action.php",
dataType: "json",
data: $("#FORMX").serialize() + "&KOPLO=tipe",
success: function(data) {
//alert(data);
//if($.trim(php_script_response) == "sukses"){
if (data.status == "sukses") {
alert('Input Berhasil');
//window.location='logout.php';
} else if (data.status == 'gagal') {
alert("Error on query!");
}
},
}); //end ajax
<script>
var btnClicked = "";
</script>
<table border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td align="center">
<input type="submit" value="SAVE CONTINUE" name="SC" id='SC' onclick="btnClicked = 'SC'" class="ui-input-btn ui-btn ui-shadow" data-role="button">
</td>
<td align="center">
<input type="submit" value="SAVE FINISH" name="SF" id='SF' onclick="btnClicked = 'SF'" class="ui-input-btn ui-btn ui-shadow" data-role="button">
</td>
</table>