Postingan lainnya
Validasi multiple rows
Mohon bantuanya Dibawah ada multiple rows dengan data Row1 = beras Row2 = beras Dari 2 data input diatas ada 2 kode id yg sama yaitu 'beras' Pertanyaan saya Bagaimana cara memberi pesan kepada user bahwa data yg di input sama sebab seharusnya setiap row data id tidak boleh sama... input data ini belum masuk kedatabase jd pesan diambil dari rows text bukan dari validasi database.. bila ada teman2 punya refernsi yg sy maksud tolong share... terima kasih
3 Jawaban:
codingan agan taruh sini saat add rows
[<!DOCTYPE html> <html> <head> <title>jQuery Add Table Rows</title> <style> table{ width: 100%; margin: 25px 0; border-collapse: collapse; } table, th, td{ border: 1px solid #6C220B; } table th, table td{ padding: 8px; text-align: left; } </style> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script> $(document).ready(function(){
$(".row").click(function(){
var name = $("#name").val();
var subject = $("#subject").val();
var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + name + "</td><td>" + subject + "</td></tr>";
$("table tbody").append(markup);
});
});
</script>
</head> <body> <form> <input type="text" id="name" placeholder="Enter Name"> <input type="text" id="subject" placeholder="Enter Subject"> <input type="button" class="row" value="Click to Add Row"> </form> <table> <thead> <tr> <th>Choose</th> <th>id</th> <th>Subject</th> </tr> </thead> <tbody> <tr> <td><input type="checkbox" name="result"></td> <td></td> <td></td> </tr> </tbody> </table> </body> </html> ]
Jawaban Terpilih
coba ini gan <pre> <!DOCTYPE html> <html> <head> <title>jQuery Add Table Rows</title> <style> table{ width: 100%; margin: 25px 0; border-collapse: collapse; } table, th, td{ border: 1px solid #6C220B; } table th, table td{ padding: 8px; text-align: left; } </style> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script> $(document).ready(function(){
$(".row").click(function(){
var name = $("#name").val();
var subject = $("#subject").val(); var ada = false; $(".name").each(function(i){ if ($(this).val()==name) { ada = true; } })
if (ada) { alert('data kembar'); } else { var markup = "<tr><td><input type='checkbox' name='record'></td><td><input type='hidden' class='name' value='"+name+"'>" + name + "</td><td class='subject'>" + subject + "</td></tr>"; $("table tbody").append(markup); }
}); }); </script> </head> <body> <form> <input type="text" id="name" placeholder="Enter Name"> <input type="text" id="subject" placeholder="Enter Subject"> <input type="button" class="row" value="Click to Add Row"> </form> <table> <thead> <tr> <th>Choose</th> <th>id</th> <th>Subject</th> </tr> </thead> <tbody> <tr> <td><input type="checkbox" name="result"></td> <td></td> <td></td> </tr> </tbody> </table> </body> </html> </pre>