首页 > 解决方案 > 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

标签: javascriptjquery

解决方案


解析两个文本字段的值,进行比较,然后用 设置第三个文本字段的值.val()

var val1 = parseFloat($("#textfield01").val());
var val2 = parseFloat($("#textfield02").val());
if (val2 > val1) {
    $("#textfield03").val(val2/2);
}

推荐阅读