根据单选添加HTML表单的动作

时间:2019-09-24 14:24:17

标签: javascript jquery html forms

我正在尝试根据用户选择的单选按钮将操作添加到HTML表单中。到目前为止,我所写的内容行之有效,只是它没有重定向到我设置的完整URL。这是我的代码:

$("input[name$='discount_options']").click(function () {
        if (document.membershipIndividualJoin.discount_options[0].checked == true) {
            document.membershipIndividualJoin.action = "https://netdonor.net/page/48617/membership/2?membershipTypeId=129";
            console.log("checked regular");
        }

        else if (document.membershipIndividualJoin.discount_options[1].checked == true) {
            document.membershipIndividualJoin.action = "https://netdonor.net/page/48625/membership/2?membershipTypeId=132";
        }

        else if (document.membershipIndividualJoin.discount_options[2].checked == true) {
            document.membershipIndividualJoin.action = "https://netdonor.net/page/48629/membership/2?membershipTypeId=133";
        }

        else if (document.membershipIndividualJoin.discount_options[3].checked == true) {
            document.membershipIndividualJoin.action = "https://netdonor.net/page/48622/membership/2?membershipTypeId=130";
        }

        return true;
    });

该页面未重定向到完整URL,而是重定向到https://netdonor.net/page/48629/membership/2,这对我而言不起作用。有什么办法可以解决这个问题?

1 个答案:

答案 0 :(得分:-1)

您可以尝试这样

    <p>Please select</p>
    <input type="radio" name="discount_options" value="1"> 1
    <input type="radio" name="discount_options" value="2"> 2
    <input type="radio" name="discount_options" value="3"> 3
    <input type="radio" name="discount_options" value="4"> 4

<script>
$("input[name='discount_options']").click(function () {
    if ($(this).attr("value") == 1) {
      window.location.href = "https://netdonor.net/page/48617/membership/2?membershipTypeId=129";
    }

    else if ($(this).attr("value") == 2) {
        window.location.href = "https://netdonor.net/page/48625/membership/2?membershipTypeId=132";
    }

    else if ($(this).attr("value") == 3) {
        window.location.href = "https://netdonor.net/page/48629/membership/2?membershipTypeId=133";
    }

    else if ($(this).attr("value") == 4) {
        window.location.href = "https://netdonor.net/page/48622/membership/2?membershipTypeId=130";
    }
});
</script>