我正在尝试选择一组单选按钮的值并使用它来设置变量。这个想法是每次更改单选按钮时变量都会更改。
由于某种原因,这似乎不起作用,我不知道为什么。该变量似乎没有从最初定义的“0”改变。按下黄色按钮时,变量应显示在警告框中。
我已经整理了我想要实现的简化版本 - 见下文。
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var paytype = 0;
$("input [name = paytype]").change(function(){
var paytype = $(this).val();
});
$("#button").click(function(){
alert ( paytype );
});
});
</script>
</head>
<body>
<form id="enrolmentform" action="thanks.php">
<input type="radio" name="paytype" id="bacs" value="Bank Transfer" />
<label for="bacs">Bank Payment/BACS Transfer</label>
<input type="radio" name="paytype" id="cheque" value="Cheque" />
<label for="cheque">Cheque</label>
<input type="radio" name="paytype" id="card" value="Card" />
<label for="card">Card</label>
<div style="width:45px; height: 20px; background-color: yellow; border: thin solid black; margin-top: 20px;"><a href="#" id="button">Button</a></div>
</form>
</body>
</html>
非常感谢任何帮助。
谢谢!