0
I have a form that creates the input fields dynamically (ASP.NET MVC C#), when the user type in a field, I want to execute an ajax to fill the other fields.
When I created a test.html, with the fixed fields and not being created dynamically, everything works!
As seen in: https://fiddle.jshell.net/nmcpjpss/3/ basically is the following code:
$(document).ready(function() {
$('.placaCss').blur('input', function() {
var valor = $(this).val();
var _this = $(this); //Esta linha retorna n.fn.init(1)
// Verificando se o valor foi preenchido
if (valor) {
var proModelo = _this.nextAll('input.marcaModeloCss:first');
proModelo.val(valor);
}
});
});
When debugging in Chrome, I realize that the _this
returns n.fn.init(1)
which I believe means he found the field. so it works.
But when I run in my dynamically created form, it returns to me n.fn.init(0), I believe that at this point jquery did not find my object in the DOM tree. Because in the test it rises the object pro DOM and in the dynamical form no?
Is there any way I can do what I want?
I also tested with other formats, but all return the wrong field in the dynamic form and correct in the test.
var t1 = _this.nextAll('.marcaModeloCss:first');
var t2 = _this.nextAll('.marcaModeloCss').first();
var t3 = _this.nextUntil().last().next();
My original question: jquery Blur in dynamic inputs
I found a similar question and it confirms that the error is because the selector was not found in the DOM https://stackoverflow.com/questions/34494873/why-is-my-jquery-selector-returning-a-n-fn-init0-and-what-is-it but does not explain a possible solution.
Update: As suggested I reloaded the Blur, but same error, to make clearer the error in the dynamic form, follow the images:
Final answer
After help from the posts below, I had to make a hybrid solution between Jquery and pure Javascript. It was not the most elegant, but unfortunately it was the only one that worked, using jquery with . nextall did not work in any way.
$(document).ready(function () {
$(document).on('blur', 'input.placaCss', function () {
var valor = $(this).val();
var _this = $(this);
// Verificando se o valor foi preenchido
if (valor) {
var campo= document.getElementsByClassName('MarcaModelo');
$.ajax({
type: "GET",
url: "/ConsultaVeiculo/Placa?placa=" + valor,
dataType: "json",
success: function (data) {
campo[0].value = data.content.bin.marca;
campo[1].value = data.content.bin.ano_fabricacao;
campo[2].value = data.content.bin.ano_modelo;
}
});
}
});
});
in short, I had to search the css for document.getElementsByClassName
that returns me an array and so work with the desired field.
Shows code when creating inputs dynamically, probably the error is there.
– Felipe Duarte
didn’t get it? , I copied the generated html for my test and it worked, the html is in the Fiddler link
– Dorathoto
would who put -1 could guide me why this question is not part of the site scope? since the American question has up.
– Dorathoto
Calm fellow rs, the line
var proModelo = _this.nextAll('input.marcaModeloCss:first');
had to return the next input of those, in the fiddle there worked, is that in your HTML they are not brothers? About thedocument.getElementsByClassName
, using as it is you are always changing the first 3 of the document, not necessarily those with respect to the input you took the value.– Dudaskank