Yoan__Fr
Yoan__Fr

Reputation: 49

jQuery get value Input Text

Here is my HTML code :

<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 :

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. I don't understand why ??

Upvotes: 0

Views: 686

Answers (1)

Satpal
Satpal

Reputation: 133403

Important You need to remove disabled attribute.

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

Use, .val() to set value. like

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

Instead of

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

.val() function gives you string. Hence you need to convert string value into appropriate one.

  1. Use parseFloat() for floating point number. operation
  2. Use parseInt() for operation on integers.

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);
}

Upvotes: 2

Related Questions