简体   繁体   English

Greasemonkey:为什么if语句总是返回true?

[英]Greasemonkey: Why does this if statement always return true?

This confuses me... I'm using waitforkeyelements on an ajax page... I have an if statement that should only pop up an alert if there is a button with the value "Accept Offer". 这使我感到困惑...我在ajax页面上使用waitforkeyelements ...我有一个if语句,仅当存在带有“ Accept Offer”值的按钮时,才应弹出警报。 Unfortunately, it's popping the alert when the only 2 buttons have values of "Continue Exploring" and "Invest". 不幸的是,当仅有两个按钮的值分别为“ Continue Exploring”和“ Invest”时,它将弹出警报。 I have a feeling it has something to do with true/false stuff, but I can't figure out why getElementById is supposed to work used like this but my selector doesn't. 我有一种感觉,它与正确/错误的内容有关,但我不知道为什么应该使用getElementById这样工作,但我的选择器却没有。 This DOES work if I add an ===something but I want to know why it doesn't work like this. 如果我添加===something这确实可行,但是我想知道为什么它不能如此工作。

So, when there are 2 buttons that have values of "Continue Exploring" and "Invest", I get an alert that says "Yes!" 因此,当有两个按钮的值分别为“ Continue Exploring”和“ Invest”时,我会收到一条警告,提示“是!”。 and then an alert that says "Invest". 然后显示“投资”的警报。 Shouldn't it run the "else" when there isn't a button with the value "Accept Offer"? 当没有带有“接受报价”值的按钮时,它不应该运行“其他”吗?

function countit () {

  if ($( '.adv-action[value*="Accept Offer"]')){

    alert("Yes!")
    alert($('.adv-action').val())

   }

  else{
    alert("no")
    alert($('.adv-action').val())
   }
}

waitForKeyElements ('[value*="Continue Exploring"]', countit);

You should do 你应该做

if ($( '.adv-action[value*="Accept Offer"]').length!=0){

    alert("Yes!")
    alert($('.adv-action').val())

   }

else{
    alert("no")
    alert($('.adv-action').val())
}

$( '.adv-action[value*="Accept Offer"]') returns an array and array will always equate to true, so check if it actually contains any element or not by equating its length to 0. $( '.adv-action[value*="Accept Offer"]')返回一个数组,该数组将始终等于true,因此请通过将其长度等于0来检查它是否真正包含任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM