如何获取取消选择的单选按钮的值?
我有几个可以动态获取其值的单选按钮。然后我有一些 javascript 来捕获这些值并将它们相加,以便它们在文本框中很好地显示。
(基本上就像计算器添加总价值的值)
现在,如果有人选择另一个单选按钮,它仍然会添加该值,但该组中最后一个按钮的值不会被减去。这就是我需要做的就是减去取消选择值。
我的表单值如下所示:“5/55” 其中 55 是我用于表示值的数字。 我正在更新的文本字段称为“通知”。所以您会看到我成功地将值添加到通知字段中并使用新选择的单选按钮值。当在组中选择新值时,如何摆脱旧值?
这是我正在使用的 javascript:
<script>
function eventTrigger (e) {
if (! e)
e = event;
return e.target || e.srcElement;
}
function radioClick (e) {
var obj = eventTrigger (e);
var notify = document.getElementById &&
document.getElementById ('notify');
if (notify)
var partsArray = obj.value.split('/');
notify.value = Number(notify.value) + Number(partsArray[1]);
return true;
}
</script>
谢谢您的帮助!
I have several radio buttons that get their value dynamically. Then I have some javascript that catches the values and adds them up so they display nicely in a text box.
(Basically like a calculator adding values for a total value)
Now, if someone selects another radio button, it still adds that value but the value of the last button in that group doesn't get subtracted. That's what I need to do is to subtract the deselected value.
My form values look like this: "5/55" Where 55 is the number I am using for my value.
The text field I am updating is called "notify". So you see I am successfully adding the value in the notify field with the newly selected radio button value. Just how do I get rid of the old value when a new one is selected in a group?
Here is the javascript I am using:
<script>
function eventTrigger (e) {
if (! e)
e = event;
return e.target || e.srcElement;
}
function radioClick (e) {
var obj = eventTrigger (e);
var notify = document.getElementById &&
document.getElementById ('notify');
if (notify)
var partsArray = obj.value.split('/');
notify.value = Number(notify.value) + Number(partsArray[1]);
return true;
}
</script>
Thank you for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保持简单:每次发生变化时,都从头开始重新计算最终结果。减去旧值会很快变得非常复杂,并且不值得,除非您需要执行非常复杂的计算,这将受益于加速。因此,每次用户更改选择时,都会根据当前选择的值计算总和。完毕。
Keep it simple: recalculate your final result from scratch every time something changes. Messing around with subtracting the old value can get really complex really quickly and is not worth it, unless you have really complex calculations to perform that would benefit from the speedup. So, every time the user changes a selection, calculate your sum based on the currently selected values. Done.