[英]How can I check to see if a string exists and change the string of another element?
Hi I'm trying to figure out how to check for the presence of a particular string, AND if that string is NOT present, change the text of another node. 嗨,我正在尝试弄清楚如何检查特定字符串的存在,如果该字符串不存在,请更改另一个节点的文本。
The following shows what I've done so far. 下面显示了我到目前为止所做的。 The problem I'm having is, my condition to execute the text change simply doesn't work. 我遇到的问题是,我执行文本更改的条件根本不起作用。 Admittedly, this is my first time using indexOf(), so I'm not certain that I'm doing so properly. 不可否认,这是我第一次使用indexOf(),所以我不确定自己做得是否正确。
// Check to see if there are non-refundable rates // If they are NOT refundable DO NOT change the name var taoIGCOR = jQuery('.rateInfoContainer[data-rate-code="RATECODE"]'); var taoRateBullet = jQuery('.rateBullet').text().indexOf('Non Refundable') <= -1; if (jQuery(taoRateBullet).text().indexOf('Non Refundable') >= 1) { // If 'Non Refundable' text IS NOT present, change it to this... jQuery(taoIGCOR).text("THIS IS THE NEW RATE NAME"); alert("Change the rate name"); } else { // If 'Non Refundable' text IS present don't do anything alert("There are refundable rates here....don't do anything."); }
.rateInfoContainer { border: 1px solid red; padding: 15px; margin-bottom:10px; } .rateName { border: 1px solid blue; padding: 15px; } .rateBullets { border: 1px solid green; padding: 15px; } .color { background: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="rateInfoContainer" data-rate-code="RATECODE"> <div class="rateName"> This text does not change <div class="rateDescription"> <ul class="rateBullets"> <li>Non Refundable</li> </ul> </div> </div> </div> <div class="parent rateInfoContainer"> <div class="textTarget rateName"> This is the target text <div class="rateDescription"> <ul class="nested rateBullets"> <li>String not present...text should change</li> </ul> </div> </div> </div> <div class="parent rateInfoContainer"> <div class="textTarget rateName"> This text does not change <div class="rateDescription"> <ul class="nested rateBullets"> <li>Non Refundable</li> </ul> </div> </div> </div>
All help/guidance is appreciated! 感谢所有帮助/指导!
indexOf()
returns the index position of a string within string, starting at position 0
. indexOf()
返回字符串在字符串中的索引位置,从位置0
开始。 If the substring can't be found, it returns -1
. 如果找不到子字符串,则返回-1
。
So, if you want to know if "Non Refundable" is in .rateBullet
, then this: 因此,如果您想知道.rateBullet
是否为“不可退款”,则此操作:
var taoRateBullet = jQuery('.rateBullet').text().indexOf('Non Refundable') <= -1;
should be this: 应该是这样的:
var taoRateBullet = jQuery('.rateBullet').text().indexOf('Non Refundable') >= -1;
And, if you want to know if "Non Refundable" is not in taoRateBullet
, then this: 而且,如果您想知道taoRateBullet
是否不存在“不可退款”,则可以执行以下操作:
if (jQuery(taoRateBullet).text().indexOf('Non Refundable') >= 1) {
should be this: 应该是这样的:
if (jQuery(taoRateBullet).text().indexOf('Non Refundable') === -1) {
Now, you've also got a few other issues, so check the comments below for details inline: 现在,您还遇到了其他一些问题,因此请查看以下注释以内联详细信息:
// It's a best-practice to name variables that hold JQuery wrapped sets // with a leading $ to remind you that they are not standard DOM objects. // You can also access JQuery with just "$" instead of "jQuery" var $taoIGCOR = $('.rateInfoContainer[data-rate-code="RATECODE"]'); // Check to see if there are non-refundable rates // If they are NOT refundable DO NOT change the name if ($('.rateBullet').text().indexOf('Non Refundable') === -1) { // If 'Non Refundable' text IS NOT present, change it to this... $taoIGCOR.text("THIS IS THE NEW RATE NAME"); alert("Change the rate name"); } else { // If 'Non Refundable' text IS present don't do anything alert("There are refundable rates here....don't do anything."); }
.rateInfoContainer { border: 1px solid red; padding: 15px; margin-bottom:10px; } .rateName { border: 1px solid blue; padding: 15px; } .rateBullets { border: 1px solid green; padding: 15px; } .color { background: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="rateInfoContainer" data-rate-code="RATECODE"> <div class="rateName"> This text does not change <div class="rateDescription"> <ul class="rateBullets"> <li>Non Refundable</li> </ul> </div> </div> </div> <div class="parent rateInfoContainer"> <div class="textTarget rateName"> This is the target text <div class="rateDescription"> <ul class="nested rateBullets"> <li>String not present...text should change</li> </ul> </div> </div> </div> <div class="parent rateInfoContainer"> <div class="textTarget rateName"> This text does not change <div class="rateDescription"> <ul class="nested rateBullets"> <li>Non Refundable</li> </ul> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.