我是javascript的新手并且遇到了问题。我试图从选定的收音机和复选框中获取滚动总数。我已经尝试了一个星期才能做到正确,但我似乎无法弄明白。
<input type="radio" name="size" value="small">
<label>Small €9.99 (perfect for a quick cup)</label>
<br>
<input type="radio" name="size" value="medium">Medium €12.99 (normal days running)
<br>
<input type="radio" name="size" value="large">Large €14.99 (for the 3 p.m slump)
<br>
<input type="radio" name="size" value="extra-Large">Extra-Large €15.99 (for those stressful days)
<br>
</p>
Total
<input type="text" readonly id="total" />
<br/>
上面是html的片段,我需要将值保留为small, medium, large and extra-large
以发送到服务器。
我知道我想要什么,但我无法解决语法问题。我正在寻求一些帮助,提前谢谢
答案 0 :(得分:0)
<input type="radio" class="radio" onclick="checkStatus" name="size" value="small">
<label>Small €9.99 (perfect for a quick cup)</label>
<br>
<input type="radio" onclick="checkStatus" name="size" class="radio" value="medium">Medium €12.99 (normal days running)
<br>
<input type="radio" onclick="checkStatus" class="radio" name="size" value="large">Large €14.99 (for the 3 p.m slump)
<br>
<input type="radio" onclick="checkStatus" class="radio" name="size" value="extra-Large">Extra-Large €15.99 (for those stressful days)
<br>
</p>
Total
<input type="text" readonly id="total" />
在javascript中写道:
function checkStatus(){
document.getElementsByClassName("radio").forEach(function(item){
if(this.checked){
alert(this.value);
}
});
}
答案 1 :(得分:0)
<input type="checkbox" name="size" value="small" price="9.99">
<label>Small €9.99 (perfect for a quick cup)</label>
<br>
<input type="checkbox" name="size" value="medium" price="12.99">Medium €12.99 (normal days running)
<br>
<input type="checkbox" name="size" value="large" price="14.99">Large €14.99 (for the 3 p.m slump)
<br>
<input type="checkbox" name="size" value="extra-Large" price="15.99">Extra-Large €15.99 (for those stressful days)
<br>
</p>
Total
<input type="text" readonly id="total" />
<br/>
$(document).ready(function() {
$("input").click(function(e){
var baseVal = parseFloat($("#total").val()) || 0;
if($(e.target).find(":checked").context.checked)
$("#total").val(baseVal+parseFloat($(e.target).attr("price")));
else
$("#total").val(baseVal-parseFloat($(e.target).attr("price")));
});
});
答案 2 :(得分:0)
处理您的HTML不符合标准
<input type="radio" name="size" value="small" onclick="setYourTotal('Small€9.99 ')" />Small €9.99 (perfect for a quick cup)
<br>
<input type="radio" name="size" value="medium" onclick="setYourTotal('Medium €12.99')" />Medium €12.99 (normal days running)
<br>
<input type="radio" name="size" value="large" onclick="setYourTotal('Large €14.99')" />Large €14.99 (for the 3 p.m slump)
<br>
<input type="radio" name="size" value="extra-Large" onclick="setYourTotal('Extra-Large €15.99')" />Extra-Large €15.99 (for those stressful days)
<br>Total
<input type="text" readonly id="total" />
简易JavaScript功能:
<script>
function setYourTotal(data) {
document.getElementById("total").value = data;
}
</script>