Nhập Input Text trên thì Input Text dưới hiện y chang
**** Cách 1:
<input name="title" id="title" type="text" class="form-control" value="Máy pha cà phê Solis Barista Perfetta Plus">
<input name="pid_link" id="pid_link" type="text" class="form-control" value="May-pha-ca-phe-Solis-Barista-Perfetta-Plus">
<script>
$(document).ready(function ($) {
function slugify(text) {
return text
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9\s-]/g, '')
.trim()
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
}
function autoSlug(titleSelector, slugSelector) {
$(document).on('input blur change', titleSelector, function () {
$(slugSelector).val(slugify($(this).val()));
});
}
// dùng
autoSlug('#title', '#pid_link');
});
</script>
#---------------------------------#
Cách 2:
<input class="form-control js-slug-title" data-slug="#pid_link">
<input id="pid_link" class="form-control">
<script>
$(document).ready(function ($) {
function slugify(text) {
return text
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9\s-]/g, '')
.trim()
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
}
$(document).on('input', '.js-slug-title', function () {
const target = $(this).data('slug');
$(target).val(slugify($(this).val()));
});
});
</script>

Không có nhận xét nào:
Đăng nhận xét