简体   繁体   English

jQuery。 脚本更改输入字段中的值。 比基于输入字段中的值需要执行if语句

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

Here is example jsFiddle 这是示例jsFiddle

If enter something in First input, then value in the Second and Third input becomes 1 (one). 如果在“第一个”输入中输入内容,则“第二个”和“第三个”输入中的值将变为1(一个)。

Then if value in Third input is > 0 then do something, for example alert("Test"); 然后,如果“第三输入”中的值> 0则执行某些操作,例如alert("Test");

But this if statement does not work. 但这if语句不起作用。 For my eyes value in Third input is 1 (one), but for script seems the value in Third input is empty 对我而言,第三输入的值是1(一),但是对于脚本来说,第三输入的值是空的

How to get execute if statement based on value in Third input? 如何获得基于第三输入值的if语句?

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) 1)在第一个输入字段中,用户输入货币名称(USD)

2) with ajax, based on currency name get currency value in the Second input 2)用ajax,根据货币名称在第二个输入中获取货币值

3) if currency value in the Second input changes, then changes Third input 3)如果第二输入中的货币值发生更改,则更改第三输入

4) and latter if user enters amount in currency in Fourth field and Third input value is > 0 , then do calculations 4)并且如果用户在第四个字段中输入货币金额并且第三个输入值> 0 ,则后者进行计算

Possible solution Here http://jsfiddle.net/eDCqN/11/ placed possible solution. 可能的解决方案这里http://jsfiddle.net/eDCqN/11/放置了可能的解决方案。 As I need if to exist / work independently, then created additional function that is executed on $("input").change . 当我需要if存在/独立工作,然后创建了上执行附加功能$("input").change So as understand each time when input changes if is executed. 因此,请理解每次执行input更改if Possibly instead of input may use some class (add class only to necessary input fields). 可能使用某些类代替input (仅将类添加到必要的输入字段中)。

Is such solution ok? 这样的解决方案可以吗?

Seems this is better (shorter) http://jsfiddle.net/NJfL3/2/ 似乎这样更好(更短) http://jsfiddle.net/NJfL3/2/

you are adding your if condition separately, you need to add it inside your change event, like: 您要分别添加if条件,则需要将其添加到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");    
    }
});

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

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