我有一个简单的2选项单选按钮。我认为我的代码可以工作并尝试过很多其他版本,但我只是遗漏了一些东西。我需要在单选按钮更改时更改var typeOfCards。该变量用于后面的等式中。
检查输入值155应将typeOfCards更改为等于boxCards和 检查输入值145应将typeOfCards更改为等于singleCards
<input name="SELECT___CD-0035___33" value="155" onclick="change_option('SELECT___CD-0035___33',this.value)" type="radio">Box Cards (Sold in Multiples of 3) <br>
<input checked="checked" name="SELECT___CD-0035___33" value="154" onclick="change_option('SELECT___CD-0035___33',this.value)" type="radio"> Single Cards (Sold in sets of 12 Cards/12 Envelopes)<br>
var singleCards = 12
var boxCards = 3
var typeOfCards = singleCards
$("input[value='155']").click(function() {
typeOfCards = boxCards;
});
$("input[value='154']").click(function() {
typeOfCards = singleCards;
});
答案 0 :(得分:0)
处理程序可能无法附加,因为它们不在document.ready中,并且脚本在创建元素之前运行。
尝试:
$(document).ready(function(){
var singleCards = 12
var boxCards = 3
var typeOfCards = singleCards
$("input[value='155']").click(function() {
typeOfCards = boxCards;
});
$("input[value='154']").click(function() {
typeOfCards = singleCards;
});
});
不过,如果你有jQuery点击处理程序,你可能也不想要onclick。
检查它,它适用于这个小提琴:http://jsfiddle.net/ccross59/4K2R7/1/
答案 1 :(得分:0)
不确定为什么你为单选按钮指定了'onclick'。
试试这个:
<script>
$(document).ready(function () {
var singleCards = 12
var boxCards = 3
var typeOfCards = singleCards
$(".radioSelect").click(function () {
if ($(this).val() == "155")
typeOfCards = boxCards;
else
typeOfCards = singleCards;
alert(typeOfCards);
});
});
</script>
<input name="SELECT___CD-0035___33" value="155" class="radioSelect" type="radio" />Box Cards (Sold in Multiples of 3) <br/>
<input checked="checked" name="SELECT___CD-0035___33" value="154" class="radioSelect" type="radio" /> Single Cards (Sold in sets of 12 Cards/12 Envelopes)<br/>