繁体   English   中英

根据单选按钮值添加到值

[英]Add to value based on radio button value

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

我有一个工作正常的脚本,根据选择的单选按钮,它将值打印到一个禁用编辑的文本框中。 我必须更进一步......我需要该值然后参与添加 function,添加单选按钮的值。

这是我尝试过的,但失败了。 没有错误,但没有添加我想要的内容。 如果我删除 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM