New in jQuery, asking for help with something that is hard for me to figure out.
The cloned row of the table contains the field <input type="text" name="dt[]" id="dt1">. Below is the template string that is being cloned.
<table id="MyTable" name="MyTable">
<tr name="tr3" id="tr3">
<td>
<input type="text" name="dt[]" id="dt1">
</td>
<td>
<input type="file" name="fff[]" id="ff1">
</td>
</tr>
</table>
The user can potentially create several of these fields, and I'm trying to figure out how to iterate over them and check if they have text before submitting the form.
Note that I must use the jQuery.on () method to access form elements. How should the loop be encoded? I originally tried this (EDITED):
$(document).on('click','#sub1',function() {
var d1 = $("[name^=dt]").val();
alert(d1);
if (d1 !=""){
$("#TheForm").submit();
} else {
alert("Empty fields!");
}
});
And this:
var d1 = $("#dt1").val();
alert(d1);
And this:
var d1 = $("#^dt").val();
alert(d1);
but could not get the data.
EDIT: upon request, this code clones the line:
$(document).on('click', '#add_row', function() {
$("table#MyTable tr:nth-child(4)")
.clone()
.show()
.find("input, span").each(function() {
$(this)
.val('')
.attr('id', function(_, id) {
var newcnt = id + count;
return id + count;
});
})
.end()
.appendTo("table")
;
count++;
if (count == 2) {
$('#add_row').prop('disabled', 'disabled');
}
});
source
share