在JavaScript中单击某个单选按钮时启用按钮

问题描述:

由于CSS,我有一个看起来像按钮的链接。我的CSS类是:在JavaScript中单击某个单选按钮时启用按钮

.classButton { 
    background: rgba(0, 0, 0, 0) linear-gradient(to bottom, #818181 0%, #656565 100%) repeat scroll 0 0; 
    border-color: #fff; 
    border-radius: 3px; 
    color: #fff; 
    font-weight: bold; 
    height: 20px; 
    padding: 10px; 
    text-decoration: none; 
    text-shadow: 0 1px 1px #000; 
    width: 100px; 
} 

的链接是:

<p class="classButton"><a href="https://www.elso.org/Excellence/AOE/StartMyApplication.aspx" style="color:#ffffff;">Start application</a></p> 

我也有JavaScript函数:

<script> 
$(document).ready(function(){ 
    $(".classButton").click(function(){ 
      window.location = "https://www.elso.org/Excellence/AOE/StartMyApplication.aspx" 
    }); 
}); 
</script> 

我要补充的两个单选按钮1.路径优秀奖2卓越。开始时应该禁用“启动应用程序”按钮。当选中其中一个单选按钮时,我必须启用此按钮,并以GET AwardType =?的形式发布。 1或2,取决于如果第一个或第二个单选按钮被选中。我怎样才能在JavaScript中做到这一点?

+0

我想我只是从你的同学那里回答同样的问题:http://*.com/a/40642405/2813224如果这有帮助,upvote它。 – zer00ne

$(document).ready(function() { 
 
    $('input[type=radio]').on('change', function() { 
 
    var url = 'https://www.elso.org/Excellence/AOE/StartMyApplication.aspx?‌​AwardType='; 
 

 
    $(".classButton").removeClass('disabled'); 
 

 
    url += $('input[type=radio]:checked').val(); 
 
    console.log(url); 
 

 
    $(".classButton a").prop('href', url); 
 
    }); 
 

 
    $(".classButton").click(function(e) { 
 
    if ($(this).hasClass('disabled')) { 
 
     e.preventDefault(); 
 
    } 
 
    }); 
 
});
.classButton { 
 
    background: rgba(0, 0, 0, 0) linear-gradient(to bottom, #818181 0%, #656565 100%) repeat scroll 0 0; 
 
    border-color: #fff; 
 
    border-radius: 3px; 
 
    color: #fff; 
 
    font-weight: bold; 
 
    text-decoration: none; 
 
    text-shadow: 0 1px 1px #000; 
 
    width: 100px; 
 
    text-align: center; 
 
} 
 

 
.classButton a { 
 
    color: #fff; 
 
    display: block; 
 
    padding: 10px; 
 
    text-decoration: none; 
 
} 
 

 
.classButton.disabled { 
 
    background: #ccc; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p class="classButton disabled"> 
 
    <a href="">Start</a> 
 
</p> 
 

 
<p> 
 
    <label for="input_1">Value 1</label> 
 
    <input type="radio" id="input_1" name="radio" value="1" /> 
 
</p> 
 

 
<p> 
 
    <label for="input_2">Value 2</label> 
 
    <input type="radio" id="input_2" name="radio" value="2" /> 
 
</p>

+0

谢谢@ZoltanToth的帮助和关注。我只是把真实的网址添加到代码中,并使用https://www.elso.org/Excellence/AOE/StartMyApplication.aspx?AwardType = AddNewModule而不是AwardType = 1或2.我找不到原因。任何想法? :) 先谢谢你。 – alenan2013

+1

我用你的真实网址更新了答案。正如你可以看到它工作得很好,所以你的问题是由你在这里发布的代码旁边的东西造成的.. –

+0

非常感谢。 – alenan2013

1)而不是在href链接的任一侧使用<p class="classButton">,使用<span class="classButton">那样<p> css不会影响按钮。

2)为按钮:<input id="submit" type="submit" onclick="check_function()">

3)为单选按钮:

<input id="radio1" type="radio" value="Path to Excellent"> 
<input id="radio2" type="radio" value="Award of Excellence"> 

3)使用Javascript:

function check_function(){ 

if (document.getElementById('radio1').checked == false) { 

     window.location.replace = "https://www.elso.org/Excellence/AOE/StartMyApplication.aspx" 
}; 

else { 

     window.location.replace = "https://www.elso.org/Excellence/AOE/StartMyApplication2.aspx" 
}; 

} 
+0

非常感谢,您的代码很有用。 – alenan2013