javascript - javascript获取选择选项值和隐藏文本字段值以进行计算
问题描述
我的表单需要执行以下计算。
选择选择菜单选项时,标题属性将成功复制到文本字段[TextField01]。
我有另一个隐藏的文本字段 [textfield02],其值由 php 查询(来自 db 的总数)确定。
我有第三个文本字段 [textfield03] 我需要根据以下条件计算。如果 [textfield02] > [textfield01] 那么 [textfield03] 的值将 = [textfield02]/2
到目前为止我工作的代码是:
jQuery(document).ready(function() {
// pass the title of the selected topic option to a hidden 'topic' form field
jQuery('select#textfield01').change(function() {
var topic = jQuery('select#textfield01 option:selected').attr('title');
// set the hidden input's value
jQuery('#textfield1').val(topic);
console.log(topic);
<select name="textfield01" id="1015" class="{validate:{required:true}}">
<option value="">Select</option>
<option value="Local_Bus" title="220.00">Local_Bus</option>
<option value="Link" title="360.00">Link</option>
</select>
我的困惑是,是否可以在上述相同的函数中复制隐藏 textfield03 的值?
我想如果我能做到这一点,我也可以添加条件和计算。
非常感谢任何为我指明正确方向的帮助。
PT
解决方案
解析两个文本字段的值,进行比较,然后用 设置第三个文本字段的值.val()
。
var val1 = parseFloat($("#textfield01").val());
var val2 = parseFloat($("#textfield02").val());
if (val2 > val1) {
$("#textfield03").val(val2/2);
}
推荐阅读
- c# - C# Sharepoint.Client - 返回给定子文件夹中的所有文件和文件夹
- vue.js - bootstrap-vue:导入特定的 css
- python-3.x - 在 windows10 上更改默认 cmd python 版本
- azure-resource-manager - Azure Policy - ARM 排除资源组
- git - 如何永久不暂存文件?
- ios - 在标签页上添加新的标签栏
- jquery - Ajax 表单发布问题
- push-notification - ReceivedRemoteNotification 和 DidReceiveRemoteNotification 的关系
- python - ValueError : 期待 ',' 分隔符:
- laravel - 包含另一个模型的模型的 Laravel 单元测试