根据 jquery 中选中的单选按钮执行不同的事件

Execute different event depending on radio button checked in jquery

我在表单中制作了一个单选按钮。
当我按下提交按钮时,我想 运行 选择 button1 时的脚本,
并且我想在没有 运行ning 的情况下发送到另一个页面(例如,上一页)选择按钮 2 时的脚本。

是否可以根据单选按钮选择不执行脚本?

html

<form class="submit-form" action="" method="post" >
    // other input tag

    <p><strong>Radio Button</strong></p>
    <div class="custom-control custom-radio">
        <input type="radio" name="option" id="option-1" class="custom-control-input" value="T" checked>
        <label class="custom-control-label" for="option-1">Radio Button 1</label>
    </div>
    <div class="custom-control custom-radio">
        <input type="radio" name="option" id="option-2" class="custom-control-input" value="F">
        <label class="custom-control-label" for="option-2">Radio Button 2</label>
    </div>

    <button type="submit">Submit</button>
</form>

脚本

<script>
    $(function () {
    var TEMP = window.TEMP;
    TEMP.init('test');
        $('.submit-form').on('submit', function (e) {
            // my logic
        });
    });
</script>

似乎一个简单的 if 语句就可以为您完成:

<script>
    $(function () {
    var TEMP = window.TEMP;
    TEMP.init('test');
        $('.submit-form').on('submit', function (e) {
            // Evaluate whether or not the radio button is checked and do the things
            let radio1 = $("#option-1");
            if (radio1.is(":checked"))
            {
                // Execute the method you want.
                doFoo();
            }
            else
            {
                // Redirect
                location.href = "new/url/goes/here";
            }
        });
    });
</script>