简体   繁体   中英

Jquery. Script changes value in input field. Than based on value in the input field need to execute if statement

Here is example jsFiddle

If enter something in First input, then value in the Second and Third input becomes 1 (one).

Then if value in Third input is > 0 then do something, for example alert("Test");

But this if statement does not work. For my eyes value in Third input is 1 (one), but for script seems the value in Third input is empty

How to get execute if statement based on value in Third input?

First input
<br>
<td class='exchange_rate1'>
<input type="text" name="exchange_rate1" id='exchange_rate1' class='exchange_rate_changed1' value="">
</td>
<br>Second input
<br>
<td>
<input type="text" name="is_exchange_rate_changed1" id="is_exchange_rate_changed1" class='exchange_rate_changed_test1'>
</td>
<br>Third input
<br>
<td>
<input type="text" name="is_exchange_rate_changed_test1" id="is_exchange_rate_changed_test1">
</td>


$(".exchange_rate_changed1").on("change", function () {
$('#is_exchange_rate_changed1').val(1).change()
});


$(".exchange_rate_changed_test1").on("change", function () {
$('#is_exchange_rate_changed_test1').val(1).change()
});

if ($("#is_exchange_rate_changed_test1").val() > 0) {
alert("Test");    
}

If question why this is necessary. Then, for example:

1) in first input field user enters currency name (USD)

2) with ajax, based on currency name get currency value in the Second input

3) if currency value in the Second input changes, then changes Third input

4) and latter if user enters amount in currency in Fourth field and Third input value is > 0 , then do calculations

Possible solution Here http://jsfiddle.net/eDCqN/11/ placed possible solution. As I need if to exist / work independently, then created additional function that is executed on $("input").change . So as understand each time when input changes if is executed. Possibly instead of input may use some class (add class only to necessary input fields).

Is such solution ok?

Seems this is better (shorter) http://jsfiddle.net/NJfL3/2/

you are adding your if condition separately, you need to add it inside your change event, like:

$(".exchange_rate_changed_test1").on("change", function () {
    $('#is_exchange_rate_changed_test1').val(1).change();
    if ($("#is_exchange_rate_changed_test1").val() > 0) {
        alert("Test");    
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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