我试图将变量从单选按钮传递给 javascript 链接中的变量。我无法将变量“testVar”(在 for 循环中检查选中的单选按钮)传递给链接。我很难过,这是完整的代码:
<form>
<label>Priority 1</label>
<input type="radio" id="priority" name="priorityN" onclick="getResults();" value="prior1" />
<label>Priority 2</label>
<input type="radio" id="priority" name="priorityN" onclick="getResults();" value="prior2" />
</form>
<br /><br />
<div id="myLink"></div>
<script>
var testVar = '';
function getResults() {
var radios = document.getElementsByName("priorityN");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
testVar = (radios[i].value);
// alert(radios[i].value);
break;
}
}
}
document.getElementById("myLink").innerHTML = '<a href=\"' + 'www.example.com?pri=' + testVar + '\" onclick=\"$(this).modal({width:880, height:460}).open(); return false;\" >' + 'My Modal Link' + "</a>";
</script>