1 回答

TA貢獻1735條經驗 獲得超5個贊
查看您的代碼,我添加了兩件事似乎可以滿足您的期望:
添加
$(document).ready(countDescriptionChar())
到您的 JS 代碼中;添加一個三元運算符,它將在您分配 var 時查找您的參數
len
,如果它返回為“未定義”,那么它應該查找您的 id<textarea>
并收集它的值var len = val === 未定義?document.getElementById('meta_description').value.length : val.value.length
$(document).ready(countDescriptionChar())
function countDescriptionChar(val) {
var len = val === undefined ?
document.getElementById('meta_description').value.length :
val.value.length
if ((len >= 0) && (len < 105)) {
$('#symbolsDescription').text(len);
document.getElementById("statusDescription").innerHTML = "<span style='color: orangered'>less then 105 characters</span>";
}
else if ((len >= 105) && (len <= 135)) {
$('#symbolsDescription').text(len);
document.getElementById("statusDescription").innerHTML = "<span style='color: green'>Idealy! (105 - 135 characters)</span>";
}
else {
$('#symbolsDescription').text(len);
document.getElementById("statusDescription").innerHTML = "<span style='color: orangered'>Too much... (more then 135)</span>";
}
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="" class="mt-4">Description</label>
<div id="statusDescription"></div>
Symbols: <div class="seocount" id="symbolsDescription"></div>
<textarea class="form-control" id="meta_description" name="meta_description" onkeyup="countDescriptionChar(this)" >
@isset ($article->meta_description)
{{ $article->meta_description}}
@endisset
</textarea>
添加回答
舉報