How to make one or more field within the screen run javascript function

Asked

Viewed 28 times

0

I have a javascript function that should be executed when any of the two fields are made any kind of change.

At first only the field #Dating when a change is performed executes the script but I want the #Quantidadeanimais also perform the function.

The question is how to do what #Dating and #Quantidadeanimais no code redundancy.

 $(document).ready(function () {
   $("#DataAbate").change(function () {
   $("#SequenciaInicial").empty();
   $("#SequenciaFinal").empty();
   $.ajax({
            type: 'POST',
            url: '@Url.Action("GetSequencia")',
            dataType: 'json',
            data: { dataAbate: $("#DataAbate").val() },
            success: function (data) {
                $.each(data, function (i, data) {

                    document.getElementById('NumeroLote').value = parseInt(data.NumeroLote) + 1;
                    document.getElementById('SequenciaInicial').value = parseInt(data.SequenciaFinal) + 1;
                    document.getElementById('SequenciaFinal').value = parseInt(data.SequenciaFinal) + parseInt($("#QuantidadeAnimais").val());
                });
            },
            error: function (ex) {
                alert('Falha ao buscar Proprietario.' + ex);
            }
        });
        return false;
    })
 )}
  • Can do $('#field, #field2') or with classnames , $('.field'), the classname can be identified in more than one element.

1 answer

1


1 -

To fire the two different id’s :

$("#DataAbate, #QuantidadeAnimais").change(function () {
//seu codigo
})

Or with classnames:

$(".classnameEmComumEntreOsCampos").change(function () {
//seu codigo
})

2 -

To add a direct classname to HTML:

<elemento class="classname"></elemento>

Or to add the classname via jQuery:

$("#DataAbate, #QuantidadeAnimais").addClass('NomeDoClassname');
  • Amigo Antraxisbr and if it is mandatory at least the field #Dataabate be filled, because it worked but only if the field #Dataabate filled.

  • @Cyberlacs which of the two ? with classname or id ? is to work with both, I will make an example with the full function working

  • With this model that worked perfectly the #Dataabate Field has to be filled -------> $("#Dataabate, #Quantidadeanimais"). change(Function() { //your code })

  • @Cyberlacs but already solved am problem ? or need to improve the answer?

  • You solved the problem yes, but the #Dataabate field priority is missing, only this one

  • Anything else I ask

  • Tell me one thing before I tidy up here, it doesn’t work if the #Dataabate field isn’t filled even when you change the other field ? and the #Dataabate field is which field ? select or input or other

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.