javascript - 如何比较两个输入值(文本和复选框)并输出结果
问题描述
我想比较这两个项目(文本和复选框)并输出结果。
例如,164cm S 是 A,186cm B 是 XL
- 输入数字为高度,单选按钮值为 3 种样式(小、正常、大)
- 输出为 164s 186b
- 我使用 a
if
并==
显示结果是 'S, M, L, XL)
如果用户输入高度并选择样式,想要输出一个与预先准备好的尺寸表相匹配的值。
我是初学者,所以这是我的计划。
这次,我要输出文本+单选按钮值
<script>
$(window).load(function(){
$(".height, .style, .style_2").keyup(function() {
var row = $(this).closest('tr');
var height = parseInt(row.find('.height').val(), 10);
var total = "height" + "style";
row.find('.total').val(isNaN(total) ? '' : total);
});
});
</script>
<table>
<tbody>
<tr id="person_total">
<td><input name="height" type="number" class="span1 height" maxlength="5"></td>
<td>
<input type="radio" id="stylebox" name="stylebox" class="style" value="s" onClick="document.getElementById('hidfield').value=this.value" />
<label for="kjc-small">S</label>
<input type="radio" id="stylebox" name="stylebox" class="style" value="n" onClick="document.getElementById('hidfield').value=this.value" />
<label for="kjc-normal">N</label>
<input type="radio" id="stylebox" name="stylebox" class="style" value="b" onClick="document.getElementById('hidfield').value=this.value" checked/>
<label for="kjc-big">B</label>
</td>
<td><input name="total" type="text" class="span1 total" readonly></td>
</tr>
</tbody>
</table>
解决方案
- 使用其他脚本控制器时不要
onClick
在 html 中使用。 input
您可以在不需要label
时插入id
id
不能复制 (id="stylebox"
)
$(document).ready(function(){
/* get nodes */
var totalNode = $('.total');
var heightNode = $('.height');
var styleNode = $('input[name="stylebox"]');
/* initial state */
var currentTotal = 0;
var currentHeight = heightNode.val();
var currentStyle = styleNode.val();
calcTotal(); // calc state
// when [input] Height change
heightNode.on('input propertychange', function() {
currentHeight = this.value;
console.log('now height', currentHeight)
calcTotal();
})
// when [radio] Style change
styleNode.change(function() {
currentStyle = this.value;
console.log('now style', currentStyle)
calcTotal();
})
// calc.
function calcTotal() {
currentTotal = currentHeight + currentStyle;
totalNode.val(currentTotal);
console.log('now total:', totalNode.val())
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<table>
<tbody>
<tr id="person_total">
<td>
<label>
<b>Height:</b>
<input name="height" type="number" class="span1 height" value="0" maxlength="5">
</label>
</td>
<!-- radio START -->
<td>
<label>
<input type="radio" name="stylebox" class="style" value="s" />
S
</label>
<label>
<input type="radio" name="stylebox" class="style" value="n" />
N
</label>
<label>
<input type="radio" name="stylebox" class="style" value="b" checked/>
B
</label>
</td>
<!-- radio END -->
<td>
<label>
<b>Total:</b>
<input name="total" type="text" class="span1 total" readonly>
</label>
</td>
</tr>
</tbody>
</table>
</body>
</html>
推荐阅读
- sql - 无法连接到 sql server:SQL server 实例失败。(系统.数据)
- android - 如何修复更新小部件的 ANR 错误
- ios - Swift UIImage全屏
- c# - 如何在 EF 6 Code First 中为经典 .NET 使用 SQL Server JSON_VALUE 函数
- node.js - 从 Node.js 应用程序而不是网站使用 FormData 调用“PostFiles”端点
- php - 如何使用 Botman.io 创建松弛对话框
- c# - FsCheck。对象与模型的区别
- ios - iOS > 8 中 TabBarVC 上的透明 vc
- heroku - Elixir POST 文件到 Heroku 文件附件扫描仪插件
- join - 在多个键上加入 clojure 集