简体   繁体   English

jQuery获取值输入文本

[英]jQuery get value Input Text

Here is my HTML code : 这是我的HTML代码:

<input type="text" name="Estimation1" id="Val1" maxlength="3" disabled=true/>
<input type="text" name="Estimation2" id="Val2" maxlength="3" disabled=true/>
<input type="text" name="Estimation3" id="Val3" maxlength="3" disabled=true/>
<input type="text" name="Estimation4" id="Val4" maxlength="3" disabled=true/>

And My jQuery : 和我的jQuery:

function CalculAuto()
{

    // get the values from this row:
    var val1 = $('#Val1').val();
    var val2 = $('#Val2').val();
    var val3 = $('#Val3').val();
    if (val1=="") val1=0;
    if (val2=="") val2=0;
    if (val3=="") val3=0;
    var total = 100 - ((val1 * 1) + (val2 * 1) +  (val3 * 1));
    // set total for the row
    $('#Val4').attr('value', total);
}

My problem is : when I send my form, $_POST['Estimation4'] is not recognized. 我的问题是:当我发送表单时,无法识别$_POST['Estimation4'] I don't understand why ?? 我不明白为什么?

Important You need to remove disabled attribute. 重要您需要删除disabled属性。

<input type="text" name="Estimation4" id="Val4" maxlength="3"/>

Use, .val() to set value. 使用.val()设置值。 like 喜欢

$('#Val4').val(total);

Instead of 代替

$('#Val4').attr('value', total);

.val() function gives you string. .val()函数为您提供字符串。 Hence you need to convert string value into appropriate one. 因此,您需要将字符串值转换为适当的值。

  1. Use parseFloat() for floating point number . 使用parseFloat()作为浮点数 operation 操作
  2. Use parseInt() for operation on integers . 使用parseInt()整数进行运算。

Complete Code 完整的代码

function CalculAuto()
{

    // get the values from this row:
    var val1 = $('#Val1').val();
    var val2 = $('#Val2').val();
    var val3 = $('#Val3').val();

    val1 = val1=="" ? 0 : parseFloat(val1);
    val2 = val2=="" ? 0 : parseFloat(val2);
    val3 = val3=="" ? 0 : parseFloat(val3);

    var total = 100 - ((val1 * 1) + (val2 * 1) +  (val3 * 1));
    // set total for the row
    $('#Val4').val(total);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM