Skip to main content
Inspiring
Mar 17, 2025 6:56 PM UTC
Answered

Selecionar checkbox consoante select

Boa noite a todos, ca estou eu a pedir a vossa preciosa ajuda.

Tenho um campo select com o nome de "tipo_servico" e vários checkbox com os nome exemplos (total, parcial, noturno, etc)

fiz o script abaixo e coloquei no campo tipo_servico em "Ações" quando o select perder o foco executar o script:

 

// Obtém o resultado do campo 'select' e seleciona o 'checkbox' correspondente
var campo1 = this.getField('tipo_servico');
var checkbox1 = this.getField('total');
var checkbox2 = this.getField('parcial');

// Função para verificar a seleção e marcar/desmarcar o checkbox2
campo1.setAction('Keystroke', function() {
    // Usando switch para verificar o valor selecionado no campo1
    switch(campo1.value) {
        case 'total':
            // Marca o checkbox total quando 'total' for selecionado
            checkbox1.checkThisBox(0, true);
            break;
        case 'parcial':
            // Marca o checkbox parcial quando 'parcial' for selecionado
            checkbox2.checkThisBox(0, true);
            break;
        default:
            // Caso nenhum item específico seja selecionado, desmarcar os checkbox
  checkbox1.checkThisBox(0, false);          
checkbox2.checkThisBox(0, false);
            break;
    }
});

Mas não funciona, o que fiz de errado? 

John280476216b9oCorrect answer
Inspiring
Mar 24, 2025 7:14 PM UTC

Vejá se fiz correcto agora, mas se fiz, mesmo assim não dá


Deixo aqui o que faltava para funcionar, não sei se foi o que indicaram.

Todas as respostas estão correctas, mas faltava selecionar o campo da imagem.

12 replies

PDF Automation Station
Community Expert
Community Expert
Mar 18, 2025 12:14 AM UTC

The cScript paramater of the field setAction method must be in quotes and you either have to remove all the line breaks, or concatenate all of them with + and quotes.

Translate
Inspiring
Mar 20, 2025 4:12 PM UTC

Boa tarde, penso eu que fiz tudo correcto.

 

Translate
Bernd Alheit
Community Expert
Community Expert
Mar 20, 2025 5:10 PM UTC

Why do you use setAction in the script?

Translate
Nesa Nurani
Community Expert
Community Expert
Mar 17, 2025 7:07 PM UTC

If you wish to use script as 'On Blur' you can use this:

var campo1 = event.target.value;
var checkbox1 = this.getField('total');
var checkbox2 = this.getField('parcial');

checkbox1.checkThisBox(0,(campo1 == "total")? true : false);
checkbox2.checkThisBox(0,(campo1 == "parcial")? true : false);
Translate
Inspiring
Mar 17, 2025 8:34 PM UTC

talvez para duas opções, sim.

Mas caso seja um select com 5 ou mais tipos de seleções talvez seja melhor usar o case

Translate
Bernd Alheit
Community Expert
Community Expert
Mar 17, 2025 7:05 PM UTC

Check the Javascript console for errors.

Translate
Inspiring
Mar 17, 2025 8:38 PM UTC

Esse é o erro:
SyntaxError: function statement requires a name

1:Field:Keystroke

SyntaxError: function statement requires a name

1:Field:Keystroke

SyntaxError: function statement requires a name

1:Field:Keystroke

Translate
PDF Automation Station
Community Expert
Community Expert
Mar 18, 2025 12:17 AM UTC

You didn't name the function.  MyFunction function().  You also need to run the function after defining it.

Translate