0
私はユーザにinput
の単語または文章を入力させ、これを他の文字列と比較します。それから私は、一致する文字列があるかどうかを確認するために、各文字列の結果に.each
を実行するので、他を続ける場合は賢明ないくつかの要素を削除します。文字列を比較すると間違った結果が出る
var text2;
var text2B;
text2 = jQuery('#usp-title').val();
text2B = text2.toLowerCase();
jQuery("#datafetch").html(data).promise().done(function() {
jQuery("#datafetch ul li h2 a").each(function() {
var $this = jQuery(this);
if ($this.text().toLowerCase().trim() !== text2B.trim()) {
$this.parent().parent().remove();
jQuery(".jumbotron").remove();
jQuery("#componi").removeAttr("disabled", "disabled").show();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="#usp-title" type="text">
jQuery(".jumbotron").remove();
はとにかく起こっています。私はすべての結果が
$this.parent().parent().remove();
で取り除かれているので私はそれを知っています、そして、私はそれらを見ず、正しい一致する文字列結果だけを残します。私も試してみました
<div id="#datafetch">
<ul>
<li>
<h2>
<a>Lorem</a>
</h2>
</li>
<li>
<h2>
<a>Ipsum</a>
</h2>
</li>
<li>
<h2>
<a>Dolor</a>
</h2>
</li>
</ul>
:
if ($thisText !== text2B.trim()) {
jQuery(".jumbotron").hide();
$this.parent().parent().remove();
jQuery("#componi").removeAttr("disabled", "disabled").show();
} else {
jQuery(".jumbotron").show();
}
'data'変数には何がありますか? – ischenkodv
あなたのコードでは、少なくとも1つの文字列が一致しないときに.jumbotronが削除されるので、もちろんあなたの例では常に削除されます。 – endrju
はい、一致するものがあってもshow()が呼び出されても、次回の比較ではhide()が呼び出されます(最後に比較された文字列が正しい場合を除きます)。 – endrju