我正在制作一个带有复选框和单选按钮的表单我使用该值作为价格,我用脚本计算,以便获得所选复选框和单选按钮的总价格。
<script>
function calcscore() {
score = 0;
$(".calc:checked").each(function () {
score += Number($(this).val());
});
$("#price").text(score.toFixed(2));
$("#sum").val(score)
}
$().ready(function () {
$(".calc").change(function () {
calcscore()
});
});
</script>
<input type="radio" id="1" value="40" class= "calc" name = "780143050"> Gezichtsbehandeling (incl.)<br>
<input type="radio" id="2" value="40" class = "calc" name = "780143050"> Massage<br>
<input type="radio" id="3" value="75" class = "calc" name = "780143050"> beide
<span id="output"></span><input type="hidden" name="sum" id="sum" value="0">
<p>Totaal extra's : € <span id="price">0</span>
我有以下PHP
测试.php
<?php
echo("keuze : " . $_POST['780143050'] . "<br />'n");
?>
我在我的 php 脚本中值和 ID。 如何回显所选单选按钮的 ID?
有两种方法可以做到这一点:
- 将
value
的值更改为id
的值 - 将
id
的值添加到value
.html:
<input type="radio" id="2" value="40|2" class = "calc" name = "780143050"> Massage<br>
.js:
<script>
function calcscore() {
score = 0;
$(".calc:checked").each(function () {
score += Number($(this).val().split('|')[0]);
});
$("#price").text(score.toFixed(2));
$("#sum").val(score)
}
$().ready(function () {
$(".calc").change(function () {
calcscore()
});
});
.php
<?php
echo("keuze : " . split('|',$_POST['780143050'])[1] . "<br />'n");
?>
请参阅下面的代码。所选单选按钮 ID 将按$_POST['selID']
接收:
$('form').submit(function(){
var selID = $('form input[type=radio]:selected').attr('id');
$(this).append('<input type="hidden" name="selID" value="'+selID+'" />');
});