从更多输入单选按钮值中获取总和
Get sum from more input radio button values
你能帮帮我吗?
这是我表格的一部分。在动态检查之后,我想要来自更多输入(type=radio)的值的总和。我想计算总价(变量 - $res)。
我知道这是字符串,但是当我将 settype 更改为 int 时,它不起作用。
请帮助我 :)
<div class="wrapper_input">
<input type="radio" name="fee" id="radio1" value="300">
<label for="radio1">D1 300,00 €</label>
</div>
<div class="wrapper_input">
<input type="radio" name="fee" id="radio2" value="150">
<label for="radio2">D2 150,00 €</label>
</div>
<script>
$('.wrapper_input input[type=radio]').on('change', function(event) {
var result = $(this).val();
$('.result').html(result);
})
</script>
<?php
$prem = "<span class='result'></span>";
?>
<br>
<div class="case2">
<input type="radio" name="fee1" id="radio1" value="300">
<label>D5 300,00 €</label>
</div>
<div class="case2">
<input type="radio" name="fee1" id="radio2" value="150">
<label for="radio6">D6 150,00 €</label>
</div>
<script>
$('.case2 input[type=radio]').on('change', function(event) {
var result2 = $(this).val();
$('.result2').html(result2);
})
</script>
<?php
$prem2 = "<span class='result2'></span>";
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
TOTAL PRICE
<?php
$res = $prem + $prem2;
echo $res;
?>
根据提问者的意见,这里有一个简单的客户端计算总价的例子:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script type="text/javascript">
var globalSum = 0;
function summerize(formfieldObj)
{
globalSum = 0;
formulaObj = $(formfieldObj).parents("form");
$(formulaObj).find("input[type='radio']:checked").each(function()
{
globalSum = parseInt($(this).val()) + globalSum;
$("#totalprice").html(globalSum);
$(formulaObj).find("input[name='total']").val(globalSum);
});
}
</script>
</head>
<body>
<form name="bla" method="post">
<div class="wrapper_input">
<input type="radio" name="fee" id="radio1" value="300" onClick="summerize(this)">
<label for="radio1">D1 300,00 €</label>
</div>
<div class="wrapper_input">
<input type="radio" name="fee" id="radio2" value="150" onClick="summerize(this)">
<label for="radio2">D2 150,00 €</label>
</div>
<br>
<div class="case2">
<input type="radio" name="fee1" id="radio3" value="300" onClick="summerize(this)">
<label for="radio3">D5 300,00 €</label>
</div>
<div class="case2">
<input type="radio" name="fee1" id="radio4" value="150" onClick="summerize(this)">
<label for="radio4">D6 150,00 €</label>
</div>
<input type="submit" value="Pay">
<input type="hidden" name="total">
</div>
</form>
<div>TOTAL PRICE:<br><span id="totalprice"></span></div>
</body>
</html>
希望它能帮助引导正确的方向。
你能帮帮我吗? 这是我表格的一部分。在动态检查之后,我想要来自更多输入(type=radio)的值的总和。我想计算总价(变量 - $res)。 我知道这是字符串,但是当我将 settype 更改为 int 时,它不起作用。 请帮助我 :)
<div class="wrapper_input">
<input type="radio" name="fee" id="radio1" value="300">
<label for="radio1">D1 300,00 €</label>
</div>
<div class="wrapper_input">
<input type="radio" name="fee" id="radio2" value="150">
<label for="radio2">D2 150,00 €</label>
</div>
<script>
$('.wrapper_input input[type=radio]').on('change', function(event) {
var result = $(this).val();
$('.result').html(result);
})
</script>
<?php
$prem = "<span class='result'></span>";
?>
<br>
<div class="case2">
<input type="radio" name="fee1" id="radio1" value="300">
<label>D5 300,00 €</label>
</div>
<div class="case2">
<input type="radio" name="fee1" id="radio2" value="150">
<label for="radio6">D6 150,00 €</label>
</div>
<script>
$('.case2 input[type=radio]').on('change', function(event) {
var result2 = $(this).val();
$('.result2').html(result2);
})
</script>
<?php
$prem2 = "<span class='result2'></span>";
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
TOTAL PRICE
<?php
$res = $prem + $prem2;
echo $res;
?>
根据提问者的意见,这里有一个简单的客户端计算总价的例子:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script type="text/javascript">
var globalSum = 0;
function summerize(formfieldObj)
{
globalSum = 0;
formulaObj = $(formfieldObj).parents("form");
$(formulaObj).find("input[type='radio']:checked").each(function()
{
globalSum = parseInt($(this).val()) + globalSum;
$("#totalprice").html(globalSum);
$(formulaObj).find("input[name='total']").val(globalSum);
});
}
</script>
</head>
<body>
<form name="bla" method="post">
<div class="wrapper_input">
<input type="radio" name="fee" id="radio1" value="300" onClick="summerize(this)">
<label for="radio1">D1 300,00 €</label>
</div>
<div class="wrapper_input">
<input type="radio" name="fee" id="radio2" value="150" onClick="summerize(this)">
<label for="radio2">D2 150,00 €</label>
</div>
<br>
<div class="case2">
<input type="radio" name="fee1" id="radio3" value="300" onClick="summerize(this)">
<label for="radio3">D5 300,00 €</label>
</div>
<div class="case2">
<input type="radio" name="fee1" id="radio4" value="150" onClick="summerize(this)">
<label for="radio4">D6 150,00 €</label>
</div>
<input type="submit" value="Pay">
<input type="hidden" name="total">
</div>
</form>
<div>TOTAL PRICE:<br><span id="totalprice"></span></div>
</body>
</html>
希望它能帮助引导正确的方向。