简体   繁体   English

JQuery 动作取决于选择了哪个单选按钮 - if/else

[英]JQuery actions depending on which radio button is selected - if/else

I believe that this should be quite simple, and I thought that I was on to something, but its just not working.我相信这应该很简单,我认为我正在做某事,但它只是不起作用。 Forgive me, as Javascript is not my forte.原谅我,因为 Javascript 不是我的强项。

I just want the main button (Get Your New Rate) to do one of two things, based on whether the user selects YES or NO to the last of the 3 questions (these are styled radio buttons).我只希望主按钮(获取您的新费率)根据用户对 3 个问题中的最后一个选择“是”还是“否”(这些是样式单选按钮)来执行以下两项操作之一。

Thanks in advance for your time!在此先感谢您的时间!

Here is the page in progress: http://atomcrayon.com/mediaforce/refinance_go_v3.1/go.html这是正在进行的页面: http : //atomcrayon.com/mediaforce/refinance_go_v3.1/go.html

<div class="radioSelection">
    <input type="radio" id="radioLicenseYes" name="radioLicense"><label for="radioLicenseYes" class="radioYes">YES</label>
    <input type="radio" id="radioLicenseNo" name="radioLicense"><label for="radioLicenseNo" class="radioNo">NO</label>
</div>

<button class="getRate">Get Your New Rate</button>

<script type='text/javascript'>
function CheckLicense() {
    var licenseYes = document.getElementById("radioLicenseYes");
    var licenseNo = document.getElementById("radioLicenseNo");
    if(licenseYes.checked) {
        $(".getRate").click(function() {
            $("#questions").fadeOut(500);
            $("#loadingAnim").delay(500).fadeIn(1).delay(7000).fadeOut(500);
            $("#loading1").delay(500).fadeIn(500).delay(1500).fadeOut(500);
            $("#loading2").delay(3000).fadeIn(500).delay(1500).fadeOut(500);
            $("#loading3").delay(5500).fadeIn(500).delay(1500).fadeOut(500);
            $("#qualify").delay(8000).fadeIn(500);
            $("#redirecting").delay(8800).fadeIn(1);
            $("#loadingAnim2").delay(8800).fadeIn(1);
        });
    }
    else if(licenseNo.checked) {
        $(".getRate").click(function() {
            $("#questions").fadeOut(500);
            $("#noQualify").delay(500).fadeIn(500);
        });
    }
}

Here a working sample:这是一个工作示例:

 $(function() { $(".getRate").click(function() { var licenseYes = document.getElementById("radioLicenseYes"); var licenseNo = document.getElementById("radioLicenseNo"); if (licenseYes.checked) { console.log("yes hello"); } else if (licenseNo.checked) { console.log("no hello"); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="radioSelection"> <input type="radio" id="radioLicenseYes" name="radioLicense"><label for="radioLicenseYes" class="radioYes">YES</label> <input type="radio" id="radioLicenseNo" name="radioLicense"><label for="radioLicenseNo" class="radioNo">NO</label> </div> <button class="getRate">Get Your New Rate</button>

With jQuery you can easily add an event listener and run your if statements.使用 jQuery,您可以轻松添加事件侦听器并运行 if 语句。 What is wrong with your code: You have created a javascript function, but you never call it!您的代码有什么问题:您创建了一个 javascript 函数,但从未调用过它! In your function, you have a bit of jquery that will do the trick just fine (with a few modifications).在你的函数中,你有一些 jquery 可以很好地完成这个技巧(有一些修改)。 You don't need to make a named function like: function getBla() .您不需要创建一个命名函数,例如: function getBla() jQuery does that for you. jQuery 为您做到了这一点。 I have simplified the code for demonstration purposes.为了演示目的,我已经简化了代码。

try:尝试:

function CheckLicense() {        
    if ($('input[name=radioLicense]:checked').length !=0 ) {
       if ($('input[name="radioLicense"]:checked').val() == "YES"){

        $(".getRate").click(function() {
            $("#questions").fadeOut(500);
            $("#loadingAnim").delay(500).fadeIn(1).delay(7000).fadeOut(500);
            $("#loading1").delay(500).fadeIn(500).delay(1500).fadeOut(500);
            $("#loading2").delay(3000).fadeIn(500).delay(1500).fadeOut(500);
            $("#loading3").delay(5500).fadeIn(500).delay(1500).fadeOut(500);
            $("#qualify").delay(8000).fadeIn(500);
            $("#redirecting").delay(8800).fadeIn(1);
            $("#loadingAnim2").delay(8800).fadeIn(1);
        });
    } else {
        $(".getRate").click(function() {
            $("#questions").fadeOut(500);
            $("#noQualify").delay(500).fadeIn(500);
        });
      }
    }
}

$('.getRate').click(function() {
     CheckLicense();
}

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

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