javascript - 我如何从输入值文本中获取?
问题描述
因此,我制作了这段代码以在客户选择按钮时动态更改价格。它有效,但现在我有一个问题,有时我不能在那里显示必须显示的价格:根据要求。
这是我的代码 HTML
<div class="reparaturwrap" style="margin:50px;">
<div class="rahmen">
<h2><b>Unser Angebot</b></h2>
<h2>{{ page_title }}</h2>
<img src="-" width="200px">
<h3 style="color:red;"><b>Totale Kosten: <span id="total"></span> CHF</b></h3>
<p>Dieses Angebot ist unverbindlich.</p>
<div class="radio-toolbar">
<input type="radio" id="form_problem_0" name="form[problem]" value="399.90">
<label for="form_problem_0" class="required" >
Display Glasbruch / Kein Bild
</label>
<input type="radio" id="form_problem_1" name="form[problem]" value="89.90">
<label for="form_problem_1" class="required" >
Akku
</label>
<input type="radio" id="form_problem_2" name="form[problem]" value="Auf Anfrage">
<label for="form_problem_2" class="required" >
Gehäuse
</label>
<input type="radio" id="form_problem_3" name="form[problem]" value="199.90">
<label for="form_problem_3" class="required" >
Rückseite
</label>
<input type="radio" id="form_problem_4" name="form[problem]" value="99.90">
<label for="form_problem_4" class="required" >
Datenrettung
</label>
<input type="radio" id="form_problem_5" name="form[problem]" value="99.90">
<label for="form_problem_5" class="required" >
Softwarefehler
</label>
<input type="radio" id="form_problem_6" name="form[problem]" value="69.90">
<label for="form_problem_6" class="required" >
Werkstattbericht Versicherung
</label>
<input type="radio" id="form_problem_7" name="form[problem]" value="99.90">
<label for="form_problem_7" class="required" >
Hintere Kamera
</label>
<input type="radio" id="form_problem_8" name="form[problem]" value="99.90">
<label for="form_problem_8" class="required" >
Vordere Kamera
</label>
<input type="radio" id="form_problem_9" name="form[problem]" value="149.90">
<label for="form_problem_9" class="required" >
Ladebuchse
</label>
</div>
<p>Wie möchten Sie weiterfahren?</p>
<div class="wrapbuttons">
<div class="radiosplit">
<button class="radio-toolbar1"><a href="/pages/einen-termin-vereinbaren">Einen Termin vereinbaren</a></button>
<button class="radio-toolbar1"><a href="/pages/per-post-einsenden">Per Post einsenden</a></button>
</div>
</div>
</div>
这是我的脚本
<script>
$('#form_problem_0').change(function(){
var form_problem_0 = parseInt($(this).val()),
total = (form_problem_0);
$('#total').html(total);
});
$('#form_problem_1').change(function(){
var form_problem_1 = parseInt($(this).val()),
total = (form_problem_1);
$('#total').html(total);
});
$('#form_problem_2').change(function(){
var form_problem_2 = parseInt($(this).val()),
total = (form_problem_2);
$('#total').html(total);
});
$('#form_problem_3').change(function(){
var form_problem_3 = parseInt($(this).val()),
total = (form_problem_3);
$('#total').html(total);
});
$('#form_problem_4').change(function(){
var form_problem_4 = parseInt($(this).val()),
total = (form_problem_4);
$('#total').html(total);
});
$('#form_problem_5').change(function(){
var form_problem_5 = parseInt($(this).val()),
total = (form_problem_5);
$('#total').html(total);
});
$('#form_problem_6').change(function(){
var form_problem_6 = parseInt($(this).val()),
total = (form_problem_6);
$('#total').html(total);
});
$('#form_problem_7').change(function(){
var form_problem_7 = parseInt($(this).val()),
total = (form_problem_7);
$('#total').html(total);
});
$('#form_problem_8').change(function(){
var form_problem_8 = parseInt($(this).val()),
total = (form_problem_8);
$('#total').html(total);
});
$('#form_problem_9').change(function(){
var form_problem_9 = parseInt($(this).val()),
total = (form_problem_9);
$('#total').html(total);
});
</script>
正如您在 form_problem_2 中看到的,该值设置为 Auf Anfrage(根据要求),但是当我单击该按钮时,它不显示任何内容。我怎样才能解决这个问题。
感谢您的帮助。问候史蒂文
解决方案
去除parseInt()
$('#form_problem_2').change(function(){
var form_problem_2 = $(this).val(),
total = (form_problem_2);
$('#total').html(total);
});
编辑:这是因为parseInt()
开头没有数字的字符串会返回NaN
含义Not a Number
,并且单词Auf Anfrage
不以数字开头
还有一种更简单的方法是这样的
$("input").change(function () {
var value = $(this).val()
if (value > 0) value = parseInt(value)
$("#total").html(value)
})
这适用于所有输入以及您可能添加的任何新输入
推荐阅读
- vba - 将幻灯片添加到部分
- javascript - 如何在羽毛笔编辑器中启用功能粘贴表
- python - 使用 with 在 Python 上读取和返回文件的单行语法
- java - Nailgun:如何运行 jar 文件?
- php - 如何通过调用 python 代码使用 PHP 将数据帧表打印到屏幕上?
- vue.js - Vuex - 在 $store.state 上获取参数
- postgresql - Kubernetes 中的 PostgreSQL HA 已复制持久卷
- python - 在scrapy上复制标题
- php - 当我尝试更新我的一个用户个人资料时,它会更新我的个人资料
- python - 我可以在 colab 中运行 python 脚本吗?