1
Guys, I’m having a problem and I still can’t find a solution, I’m still new to jquery, I’d like a little help, please. In the code below I have a procedure to call a dialog, which will be loaded with a table filled with php. This table has an input text field for search and all the lines have a checkbox, for when selected and the button pressed, the name is sent to the previous page. Everything is working perfectly, but I have to repeat the code 3 times, if you want to use in 3 different fields. I wonder how I can pass the id of the clicked button and from it do an if and execute the procedure. It should be something simple, but I haven’t yet. Thank you
<script>
$(document).ready(function(){
$("#buttonP1").click(function() {getValueUsingClass1();});
$("#buttonP2").click(function() {getValueUsingClass2();});
$("#buttonP3").click(function() {getValueUsingClass3();});
function getValueUsingClass1(){
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function() {
chkArray.push($(this).val());
});
var teste = $(this).id;
alert(teste);
/* we join the array separated by the comma */
var selected;
selected = chkArray.join('') + "";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if(selected.length > 1){
if(chkArray.length > 1){
alert("Selecione um campo");
$('input[id=TP_ACO_P]').val("");
}else{
$('input[id=TP_ACO_P]').val(selected);}
}else{
alert("Nenhuma dado selecionado");
}
$('input[id=buscar]').val("");
}
function getValueUsingClass2(){
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function() {
chkArray.push($(this).val());
});
/* we join the array separated by the comma */
var selected;
selected = chkArray.join('') + "";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if(selected.length > 1){
if(chkArray.length > 1){
alert("Selecione um campo");
$('input[id=ACAB_P]').val("");
}else{
$('input[id=ACAB_P]').val(selected);}
}else{
alert("Nenhuma dado selecionado");
}
}
function getValueUsingClass3(){
/* declare an checkbox array */
var chkArray = [];
/* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
$(".chk:checked").each(function() {
chkArray.push($(this).val());
});
/* we join the array separated by the comma */
var selected;
selected = chkArray.join('') + "";
/* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
if(selected.length > 1){
if(chkArray.length > 1){
alert("Selecione um campo");
$('input[id=TT_P]').val("");
}else{
$('input[id=TT_P]').val(selected);}
}else{
alert("Nenhuma dado selecionado");
}
$('input[id=buscar]').val("");
}
$(function() {
$( "#dialogP1" ).dialog({
resizable: false,
height:400,
width:500,
modal: true,
autoOpen: false,
open: function(event, ui) {
$('.ui-dialog-titlebar').display("none");
}
});
$( "#openerP1" ).click(function() {
$('input:checkbox').attr('checked',false);
$( "#dialogP1" ).dialog( "open" );
});
$( "#buttonP1" ).click(function() {
$( "#dialogP1" ).dialog( "close" );
});
});
$(function(){$("#dialogP1").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove();});
$(function() {
$( "#dialogP2" ).dialog({
resizable: false,
height:400,
width:500,
modal: true,
autoOpen: false,
open: function(event, ui) {
$('.ui-dialog-titlebar').display("none");
}
});
$( "#openerP2" ).click(function() {
$('input:checkbox').attr('checked',false);
$( "#dialogP2" ).dialog( "open" );
});
$( "#buttonP2" ).click(function() {
$( "#dialogP2" ).dialog( "close" );
});
});
$(function(){$("#dialogP2").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove();});
$(function() {
$( "#dialogP3" ).dialog({
resizable: false,
height:400,
width:500,
modal: true,
autoOpen: false,
open: function(event, ui) {
$('.ui-dialog-titlebar').display("none");
}
});
$( "#openerP3" ).click(function() {
$('input:checkbox').attr('checked',false);
$( "#dialogP3" ).dialog( "open" );
});
$( "#buttonP3" ).click(function() {
$( "#dialogP3" ).dialog( "close" );
});
});
$(function(){$("#dialogP3").dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove();});
});
</script>
Thank you for the answer, it’s working properly now.
– Thiago