根据单选按钮值添加到值

Add to value based on radio button value

本文关键字:添加 单选按钮      更新时间:2023-09-26

这很难描述,所以我会尽力。

我有一个运行良好的脚本,基于所选的单选按钮,它将值打印到一个禁用编辑的文本框中。我必须更进一步。。。然后,我需要将该值包含在添加函数中,添加单选按钮的值。

这就是我尝试过的,但失败了。没有错误,但没有添加我想要的内容。如果我删除If语句,它会工作,但只打印单选按钮的初始值。我需要根据所选内容添加该值。

编辑:根据请求添加html。

<center>
Choice1 : <input type="radio" name="region" id="choice1" value="10.25">
Choice2 : <input type="radio" name="region" id="choice2" value="11.25">
Choice3 : <input type="radio" name="region" id="choice3" value="8.25">
</center>
//Enter how many in this box
How many?  <input type="text" id="addtl" size="3" maxlength="2"></input> @ 
//this is the choice, based on radio button, add something to it
<input type="text" id="add_cost" size="2" maxlength="6"></input>
<script>
//print in the box 
$('input[name="region"]').change(function(){
$('#add_cost').val($('input[name="region"]:checked').val());
if(add_cost == 11.25)  {
  add_cost + 4;
}
else if (add_cost == 10.25){
  add_cost + 1;
}
else if(add_cost == 8.25){
  add_cost + 3;
}
});

</script>

如果我没有误解你的意图,我会重写你的脚本代码;

$('input[name="region"]').change(function() {
        var add_cost = parseFloat($('input[name="region"]:checked').val());
        if (add_cost == 11.25) {
            add_cost += 4;
        } else if (add_cost == 10.25) {
            add_cost += 1;
        } else if (add_cost == 8.25) {
            add_cost += 3;
        }
        $('#add_cost').val(add_cost);
    });

您似乎只是通过"add_cost"来引用add_cost文本框。除非您已经用该名称声明了一个变量,否则您必须继续寻址$("#add_cost").val()以引用其值。或者简单地声明这样一个变量,包含$("#add_cost").val().

$('#add_cost').val($('input[name="region"]:checked').val());
var add_cost = parseFloat($("#add_cost").val());
if(add_cost == 11.25)  {
  add_cost + 4;
}