Reputation: 144
Adding of text field are generated by jquery. I just want to enable the submit button after the input text field are typed in. Thanks. Here's the code: https://jsfiddle.net/akoni/kL8jdpdc/
I tried this code, but no luck.
(function() {
$('form > input').keyup(function() {
var empty = false;
$('body').find('input[type="text"]').each(function() {
if (($(this).val() == '')) {
empty = true;
}
});
if (empty) {
$('#submit').attr('disabled', 'disabled');
} else {
$('#submit').removeAttr('disabled');
}
});
})()
Upvotes: 1
Views: 1879
Reputation: 4513
The issue that you actually faced explained here, basically you should use on()
instead of keyup()
. And
input[type="text"]
will return less count then form > input
, here is the changes
$(document).on("keyup", "input[type='text']", function() {
var empty = false;
$('input[type="text"]').each(function() {
if (($(this).val() == '')) {
empty = true;
}
});
if (empty) {
$('#submit').attr('disabled', 'disabled');
} else {
$('#submit').removeAttr('disabled');
}
});
Hope helps,
Upvotes: 2
Reputation: 1969
You must use delegation binding for any html elements that is dynamically added.
Try change
$('form > input').keyup(function() {
to
$('form').on('keyup','input',function() {
Good luck!
Upvotes: 1
Reputation: 12181
Here you go with a solution https://jsfiddle.net/kL8jdpdc/9/
(function() {
$('form').on('keyup', 'input[type="text"]', function() {
var empty = false;
$('input[type="text"]').each(function() {
if (($(this).val() == '')) {
empty = true;
}
});
if (empty) {
$('#submit').attr('disabled', 'disabled');
} else {
$('#submit').removeAttr('disabled');
}
});
})()
var i=1;
$(".addmore").on('click',function(){
$('#submit').attr('disabled', 'disabled');
count=$('table tr').length;
var data="<tr><td><input type='checkbox' class='case'/></td>";
data +="<td><input type='hidden' id='process_id"+i+"' name='process_name"+i+"'/>P"+i+"</td><td><input type='text' id='burst_id"+i+"' class='text-input-grey' name='burst_name"+i+"'/></td></tr>";
$('table').append(data);
i++;
});
function select_all() {
$('input[class=case]:checkbox').each(function(){
if($('input[class=check_all]:checkbox:checked').length == 0){
$(this).prop("checked", false);
} else {
$(this).prop("checked", true);
}
});
}
$(".delete").on('click', function() {
$('.case:checkbox:checked').parents("tr").remove();
$('.check_all').prop("checked", false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method='post' action='' class='subscription-table'>
<table width="500" align='center' border="1" cellpadding="10" cellspacing="5">
<tr>
<th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
<th>Process</th>
<th>Burst Time</th>
</tr>
</table>
<button type="button" class='delete'>- Delete</button>
<button type="button" class='addmore'>+ Add Process</button>
<input id="submit" type="submit" name="submit" disabled="disabled"/>
</form>
Use $('form').on('keyup', 'input[type="text"]'
instead of $('form > input')
, if you select only input
then it will select checkboxes as well.
When you add a new row, you need to disable the submit button as well $('#submit').attr('disabled', 'disabled');
added to the click event of .addmore
button.
Hope this will help you.
Upvotes: 0
Reputation: 193
This should right about do it:
(function() {
$("form > input").keyup(function() {
var text = $("form > input").val();
if (text == "") {
$('#submit').attr('disabled', 'disabled');
} else {
$('#submit').removeAttr('disabled');
}
});
})()
Upvotes: 0