次のスクリプトを書くより効率的な方法はありますか?それがしているのは、アイテムの数を更新し、その数に応じてその数の単数または複数のテキストを表示することです。jQueryでより効率的な隠蔽/表示ができますか?
コードは、「これを表示してこれを隠す」と言うのは冗長なようです。それに基づいて表示または非表示にするべきものにブール値を渡す方法はありませんか?
var includedCount = $("#services").find("tbody tr td:nth-child(1) :checkbox:checked").length;
var requiredCount = $("#services").find("tbody tr td:nth-child(2) :checkbox:checked").length;
$("#js-included-num").html(includedCount);
if(includedCount === 1){
$("#js-included-plural").hide();
$("#js-included-singular").show();
}else{
$("#js-included-plural").show();
$("#js-included-singular").hide();
}
$("#js-required-num").html(requiredCount);
if(requiredCount === 1){
$("#js-required-plural").hide();
$("#js-required-singular").show();
}else{
$("#js-required-plural").show();
$("#js-required-singular").hide();
}
素晴らしい感謝。これは素晴らしいです。私はそれを少し修正しましたが、これがその基礎です。 –