0
the following code was implemented from a question I found here in the system
$('form input[type="text"]').on('input', function() {
var inputempty = false;
$('form > input').each(function() {
if($(this).val() == '') {
inputempty = true; } });
if(inputempty) {
$('form input[type="submit"]').attr('style', 'background: red');
} else {
$('form input[type="submit"]').removeAttr('style'); } });
the problem is that I can’t get it to read all inputs inside the form, it can only detect the first input, I’m using jquery 1.8.3 and jquery ui 1.11.4
follows html structure
<form action="<?php echo htmlspecialchars($siteuri); ?>" method="post">
<input type="text" name="contacts_form_name" placeholder="Nome do contato" autofocus />
<input type="text" name="contacts_form_a[1]" placeholder="Nome do campo" tabindex="1" />
<input type="text" name="contacts_form_b[1]" placeholder="Valor do campo" tabindex="2" />
<div id="contacts_form">
Aqui dentro são inseridos inputs com jquery
</div>
<div id="contacts_form_add" class="cursor_pointer">+</div>
<input class="cursor_pointer" type="submit" name="contact_send" value="Salvar" />
</form>
and follow the jquery
var scntDiv = $('#contacts_form');
var rscn = $('#contacts_form x').size() + 1;
$('#contacts_form_add').live('click', function() {
$('<x><input type="text" id="focus' + rscn +'" name="contacts_form_a[' + rscn +']" placeholder="Nome do campo" /><input type="text" name="contacts_form_b[' + rscn +']" placeholder="Valor do campo" /><span id="contacts_form_rem" class="cursor_pointer">X</span></x>').appendTo(scntDiv);
$('#focus'+ rscn).focus();
rscn++;
inputattr();
return false; });
$('#contacts_form_rem').live('click', function() {
if(rscn > 1) {
$(this).parents('x').remove();
rscn--; }
return false; });
What is the structure of your HTML?
– Sergio
structure are dynamic inputs, I have a jquery function that inserts inputs into a div, but even with inputs outside this block it only reads the first
– flourigh
What gives you
alert($('form > input').length);
if you put it in the first line beforevar inputempty = false;
?– Sergio
only the number 4 appears in all inputs, but only the first input that makes Submit red
– flourigh
appears only number 4 in all inputs, and do not know what happened, but apparently it is working now, I removed the code, I put again to test what you asked, I think it is working.
– flourigh
actually, it’s working on the first 3 inputs, I don’t know what happened, really weird, but the dynamic inputs aren’t getting the function, is it because they’re inside a div inside a tag? #contacts_form > x > input[type="text"]
– flourigh