在 IE 11 中获取未定义的 javascript 函数
Get undefined javascript function in IE 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>
我从这段代码中调用了 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>
我的问题是,当我试图在函数 test
顶部显示 alert
时,我得到了 undefined function test
,但如果我关闭 var total_room_qty = calculate_amount(currency); // total all room
,警报可以在 IE 中显示11 浏览器。这怎么可能发生,我该如何解决?谢谢
calculate_amount 的函数声明错误。您正在尝试在 ie11 中使用 es6。
function calculate_amount(currency = 'IDR') // this will work only in chrome and firefox
不幸的是,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;
}
我也遇到了类似的错误,Google 把我带到了这里。
在我的案例中,这也是与旧版 IE 的语法不兼容问题:不支持使用 arrow functions (=>
)。
求解见。
不幸的是,IE 告诉我这个表达式下面的函数是未定义的,所以我花了一段时间才找出真正的错误是什么。
我希望这可以为遇到同样问题的其他人节省时间。
我在 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>
我从这段代码中调用了 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>
我的问题是,当我试图在函数 test
顶部显示 alert
时,我得到了 undefined function test
,但如果我关闭 var total_room_qty = calculate_amount(currency); // total all room
,警报可以在 IE 中显示11 浏览器。这怎么可能发生,我该如何解决?谢谢
calculate_amount 的函数声明错误。您正在尝试在 ie11 中使用 es6。
function calculate_amount(currency = 'IDR') // this will work only in chrome and firefox
不幸的是,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;
}
我也遇到了类似的错误,Google 把我带到了这里。
在我的案例中,这也是与旧版 IE 的语法不兼容问题:不支持使用 arrow functions (=>
)。
求解见
不幸的是,IE 告诉我这个表达式下面的函数是未定义的,所以我花了一段时间才找出真正的错误是什么。
我希望这可以为遇到同样问题的其他人节省时间。