简体   繁体   English

如何检查字符串是否存在并更改另一个元素的字符串?

[英]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! 感谢所有帮助/指导!

  • Brion 布里昂

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.

相关问题 如何检查 URL 是否存在? - How can I check to see if a URL exists or not? 如何检查这些字符串中的任何一个是否存在于另一个字符串中,如果 JavaScript 中的答案是肯定的,则将其替换? - How to check to see if any of these strings exists inside another string and replace it if the answer is yes in JavaScript? 如何检查一串字,看它们是否是一个字的字谜 - How can I check a string of words to see if they are an anagram of one word JavaScript-如何检查一个字符串是否等于或等于另一个字符串 - JavaScript - how can I check if a string is part or equal to another string 如何检查一个字符串是否不包含另一个字符串的文本? - How can I check that a string does not include the text of another string? 检查一个字符串是否包含另一个字符串的组合 - Check to see if a string contains a combination of another string 检查字符串是否存在于另一个字符串中(不完全相等) - Check if string exists in another string (not exactly equal) 如何检查字符串的字符是否已经是数组的元素? - How can I check if a character of a string is already a element of an array? 如何检查数组元素是否存在于Javascript字符串中? - How to check whether an element of array exists in string in Javascript? Javascript - 如何检查带有类的元素是否存在于普通字符串中? - Javascript - How to check if element with a class exists in a plain string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM