简体   繁体   English

如何在单选按钮检查上运行jQuery脚本

[英]How to run a jquery script on radio button check

I'm am trying to run 2 comparison scripts when the radio button of each flight is clicked. 单击每个航班的单选按钮时,我正在尝试运行2个比较脚本。

The //Compare airports part will compare whether the passenger will return to the same airport he departed from when clicking the equivalent booking radio button and show an alert in case he will be returning to a different airport. //“比较机场”部分将比较当单击等效的预订单选按钮时乘客是否将返回原出发地的同一机场,并显示警报,以防他将返回不同的机场。

And in the same way the //Compare Dates part will check whether his flight will arrive the next day and if yes show an alert stating just that. 并且//比较日期部分将以相同的方式检查他的航班是否会在第二天到达,如果是,则显示一条警告说明。

I made a bare bones mockup of my code in codepen so you can have a clearer view of what i'm trying to do. 我在Codepen中制作了代码的基本模型,因此您可以更清楚地了解我要执行的操作。

Thanks for taking the time to help! 感谢您抽出宝贵的时间来帮助您!

http://codepen.io/JohnD/pen/myGZKP http://codepen.io/JohnD/pen/myGZKP

//Compare airports
var a1 = $(".flight-1_1 > .departing-IATA").text();
var a2 = $(".flight-2_1 > .departing-IATA").text();

if (a1 != a2) {
      $('.da-alert').toggle();
};
//Compare dates 
var day1 = $(".flight-1_1 > .date").text();
var day2 = $(".flight-2_1 > .date").text();

if (day1 != day2) {
  $(".dd-alert").toggle();
}

I understand that you want to know way to run the code when radio button's are being chacked. 我知道您想知道在单选按钮被破解时运行代码的方式。

if you add new changed event to radio button, you will see the running code when the buttons are checked. 如果将新的更改事件添加到单选按钮,则在选中按钮时将看到运行代码。

var airport1 = null
var airport2 = null
$('#radio_jfk_button').change(
    function() {
        airport1 = "jfk"
        if(airport1 != null && airport2 != null) {
            //compare
        }
    }
}
$('#radio_narita_button').change(
    function() {
        airport1 = "narita"
        if(airport1 != null && airport2 != null) {
            //compare
        }
    }
}

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

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