10
I have a problem which is this, I have the fields: Name, Age and Text;
In a part of the system I register that the Text field will only appear if the Name field is equal to PH
and the Age field have the value 20
with the two true conditions I then release the Text field.
My difficulty in this part is because it can have many fields because the form is assembled by the client, that is, the client can register 20, 30 fields for this form and all have a specific rule to release some other field. Is there any plugin that checks or that makes this type of validation?
At that moment I tried the following, but only validate this way if it was 1 to 1. But in this case it can be N-1
or 1-N
or it would be better to create an intermediate table for such an action?
$(function () {
var campoDependente = '#codigo_<?php echo $condicao->atividade_campo_dependente ?>',
campoTrigger = '#codigo_<?php echo $condicao->atividade_campo ?>',
valor = '<?php echo trim($condicao->condicao) ?>';
function mostrar() {
var val = '';
var checkbox = $(campoTrigger + ' input:checked');
var select = $(campoTrigger + ' select > option:selected');
var input = $(campoTrigger + ' input');
if (checkbox.length > 0) {
val = $.trim(checkbox.parent().text());
} else if (select.length > 0) {
val = $.trim(select.text());
} else {
val = input.val();
}
if (val == valor) {
$(campoDependente).css('display', 'inline-block');
} else {
$(campoDependente).css('display', 'none');
}
}
mostrar();
$(campoTrigger).change(mostrar);
});
Just one more thing I didn’t mention and when the fields aren’t just input’s I tried to put here in variable values in the checker function I tried to group the selectors but unsuccessfully for example I have fields that are selected in this precise case from . text and not from . value as you would in that case ? kind of take the text of the fields universally and got good the good initiative code to put it on github
– Paulo Henrique
@Paulohenrique and in this case the
option
havevalue
? if they have no valueselect.value
returns the text/.innerHTML
– Sergio
in this case the options have value yes, example:
sim
has value27
the numbers in the values are represented the ID in the database, if the client selects one of them I have to save in the bank and then bring marked the respective value and the.innerHTML
for some reason behind the input as empty even filled.– Paulo Henrique