简体   繁体   English

调用函数时拆分函数不起作用

[英]Split function does not work when calling function

I am using the Split function to take a selected option value and split by each pipe delimeter. 我正在使用“拆分”功能获取一个选定的选项值,并按每个管道的分度值进行拆分。

The value of exploded[2] is correctly shown as 10.00 when outputted but the finance function does not return a value and my div does not get updated with fresh text. 输出时,exploded [2]的值正确显示为10.00,但财务函数未返回值,并且我的div不会更新为新文本。

If I swap exploded[2] to a literal 10.00 within the function like this: 如果我在这样的函数中将exploded [2]交换为原义10.00:

finance(exploded[0], exploded[1], 10.00, exploded[3]); 

it works. 有用。 Similarly, if I make exploded[2] = 10.00 like this: 同样,如果我像这样使exploded [2] = 10.00:

var test = 10.00;
finance(exploded[0], exploded[1], test, exploded[3]);

it works too. 它也可以。 Have I done something stupid? 我做过蠢事吗?

The Javascript Javascript

<script type="text/javascript">

$(document).ready(function() {

  $('#finance').change(function() {     
    selected_value = $('#finance').val();
    exploded = selected_value.split('|');
    finance(exploded[0], exploded[1], exploded[2], exploded[3]);    
  });

  function finance(code, order_cost, depost_pc, deposit_val) {
    my_fd_obj = new FinanceDetails(code, order_cost, depost_pc, deposit_val);
    $('#finance_per_month').html(my_fd_obj.m_inst);     
  }

  });

</script>

The HTML HTML

<select name='finance' id='finance'>
  <option value="ONIF10|399.00|10.00|39.90">10 Months 0% Finance</option>
  <option value="ONIF18|399.00|10.00|39.90">18 Months 0% Finance</option>
  <option value="ONIF24|399.00|10.00|39.90">24 Months 0% Finance</option>
  <option value="ISIB36-3.9|399.00|10.00|39.90">36 Months 3.9% Finance</option>
</select>

Impossible to say for sure without knowing more about FinanceDetails , but the difference between exploded[2] and 10.00 is that the former is a string ( "10.00" , not 10.00 ). 在不进一步了解FinanceDetails情况下无法肯定地说,但是exploded[2]10.00之间的区别在于前者是字符串"10.00"而不是10.00 )。 99% of the time, code will implicitly coerce strings to numbers, but again not knowing what's in FinanceDetails ... 99%的时间,代码会将字符串隐式地强制转换为数字,但是又一次不知道FinanceDetailsFinanceDetails什么...

parseFloat(exploded[2]) might solve the problem. parseFloat(exploded[2])可能会解决此问题。 Or just +exploded[2] . 或者只是+exploded[2]

I'd expect you would need to do this for the other args as well, if they're also meant to be numbers. 我希望您也需要对其他args执行此操作,如果它们也必须是数字。

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

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