在IE 11中获取未定义的javascript函数

Ant*_*nio 1 javascript jquery function internet-explorer-11

在IE 11中调用函数时出现一些错误。在我的情况下,我在一个<script>标记中包含两个脚本。

这是我的功能:

<script>
function test()
{
    alert("oke");
    var currency = $('#curr_drop').val();
    var total_room_qty = calculate_amount(currency); // total all room

    var btn_class = $('#book_button').prop('class');

    var this_qty = $(this).val();
    var this_plan_type = $(this).data('plan-type');
    var this_plan_id = $(this).data('plan-id');
    var this_plan_day = $(this).data('plan-day');
}

function calculate_amount(currency = 'IDR') 
{
    //alert("ok");
    var nights = $('[name="nights"]').val();
    var total_amount = 0; var total_room_qty = 0; var total_qty_room_id = 0;

    console.log('nights: '+nights);


    return total_room_qty;
}
</script>
Run Code Online (Sandbox Code Playgroud)

test从以下代码调用该函数:

<select name="qty" class="mb10" onchange="test.bind(this)()" data-plan-type="rate" data-plan-id="38" data-plan-day="52459">
   <option value="0">0 rooms</option>
   <option value="1">1 rooms</option>
   <option value="2">2 rooms</option>
   <option value="3">3 rooms</option>
   <option value="4">4 rooms</option>
   <option value="5">5 rooms</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我试图表明alert在功能上面testundefined function test,但如果我关闭var total_room_qty = calculate_amount(currency); // total all room了警报可以在IE浏览器11中显示。这怎么可能发生,我该如何解决?谢谢

kar*_*ick 6

Calculation_amount的函数声明错误。您正在尝试在ie11中使用es6。

function calculate_amount(currency = 'IDR') // this will work only in chrome and firefox 
Run Code Online (Sandbox Code Playgroud)

不幸的是,IE并没有抛出错误。这就是您的事件处理程序无法访问的原因。

更改为

function calculate_amount(currency) 
{
    currency = currency || 'IDR'; //if your intent is to set a default value for currency
    //alert("ok");
    var nights = $('[name="nights"]').val();
    var total_amount = 0; var total_room_qty = 0; var total_qty_room_id = 0;

    console.log('nights: '+nights);


    return total_room_qty;
}
Run Code Online (Sandbox Code Playgroud)

范例 https://jsfiddle.net/karthick6891/0681reux/