简体   繁体   English

不调试萤火虫中的javascript代码会触发错误,否则可以正常运行

[英]not debugging javascript code in firebug fire the error otherwise works perfectly

I am sure this question seem silly to many of you but it really happen for me, the code I shared below works perfectly when I debug it thoroughly in firebug if not, and just running the code without debugging it raise an error, 我敢肯定,这个问题对你们中的许多人来说似乎很愚蠢,但是对我来说确实如此,当我在firebug中彻底调试(如果没有调试的话)下面的共享代码可以完美地运行,并且在不调试的情况下运行代码会引发错误,

 function edit(Barcode) { $('#pnlSize').show('fast'); if ($('#test2 option').size() < 2) { $.ajax({ url: '@Url.Action("selectedItemChanged")', type: 'GET', dataType: "JSON", data: { barcode: Barcode }, success: function(sizes) { var options = '<option value="Z">سایز را انتخاب کنید</option>'; for (var i = 0; i < sizes.length; i++) { console.log(sizes); options += '<option value="' + sizes[i].ID + '">' + sizes[i].Size + '</option>'; } $("#test2").html(options); } }); } var val = $('#test2 :selected').text(); if (val != 'سایز را انتخاب کنید' && val != null) { location.href = '@Url.Action("AddToCart", "ShoppingCart")?Barcode=' + Barcode + '&size=' + $('#test2 :selected').text();; } } 

the reason is that when do not debug it, it tries to run this : 原因是,当不调试它时,它将尝试运行以下命令:

  location.href = '@Url.Action("AddToCart", "ShoppingCart")?Barcode=' + Barcode + '&size=' + $('#test2 :selected').text();; 

but as dropdown has not being filled yet and user has not chosen any size, so addtoCart return null. 但由于下拉列表尚未填写,并且用户尚未选择任何大小,因此addtoCart返回null。

Please kindly help me with this 请帮助我

Move that code inside your success function or add a timer to wait until the success call has completed. 在成功函数中移动该代码,或添加计时器以等待成功调用完成。 The success callback is firing after the rest of your code runs which is why your addtoCart is null. 在其余代码运行之后,将触发成功回调,这就是为什么addtoCart为null的原因。

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

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