简体   繁体   English

IE9中的JavaScript随机失败/ IE9中过早关闭包含DIV的FORM标记

[英]JavaScript Failing in IE9 Randomly / FORM Tag Containing DIV Closing Prematurely in IE9

The general gist of the problem is that random commands seem to not work in IE9, or only work occasionally. 问题的总体要点是随机命令似乎在IE9中不起作用,或者仅偶尔起作用。 Commands that work on other pages just fine. 在其他页面上运行的命令也很好。

I've excluded some things I didn't need so now there are only two javascripts on the page. 我排除了一些我不需要的东西,所以现在页面上只有两个JavaScript。 One is jQuery 2.0.3, and the other is the pages main action script: 一个是jQuery 2.0.3,另一个是页面主要操作脚本:

$(function()
    {
    $("#lib").addClass("this_page");

    $(".output").each(convert_values);

    $("form .input_class").keyup(convert_values);
    $("form .input_class").change(convert_values);
    $(".swap").click(function($e)
        {
        var $form = $(this).closest("form");        
        var $input_value = $form.find(".input_units").val();
        var $output_value = $form.find(".output_units").val();
        $form.find(".input_units").val($output_value);
        $form.find(".output_units").val($input_value).change();
        $e.preventDefault();
        });
    });


function convert_values()
    {
    var $form = $(this).parent();
    var $input = $form.find(".input").val();
    $form.find(".input").removeClass("input_error");
    var $input_units = $form.find(".input_units").val();
    var $output_units = $form.find(".output_units").val();
    var $result = 0;

    if ($input == "") { $input = "0"; }

    console.log("data-type='"+$form.attr("data-type")+"'");
    if ( $form.attr("data-type") == "temp" )
        {
        switch ( String($input_units) + String($output_units) )
            {
            case "fc":
                $result = (Number($input) - 32) * (5/9); break;
            case "fk":
                $result = ((Number($input) - 32) * (5/9)) + 273.15; break;
            case "cf":
                $result = (Number($input) * 1.8) + 32; break;
            case "ck":
                $result = Number($input) + 273.15; break;
            case "kc":
                $result = Number($input) - 273.15; break;
            case "kf":
                $result = ((Number($input) - 273.15) * 1.8) + 32; break;
            default:
                $result = $input;
            }
        }
    else
        {
        console.log("("+$input+" / "+$output_units+") * "+$input_units);
        $result = (Number($input) / Number($output_units)) * Number($input_units);
        }

    if (isNaN($result))
        {
        //play_a_sound("snd_fail");
        $form.find(".input").addClass("input_error");
        $result = "Input Error";        
        }

    $form.find(".output").val($result);
    }

The results of this are very strange. 这样的结果非常奇怪。 My convert_values( ) function is called several times at page load, and it seems to work fine. 我的convert_values()函数在页面加载时被调用了几次,并且看起来工作正常。 But then when you call it by clicking on selects after page load, it doesn't work. 但是,当您在页面加载后通过单击selects调用它时,它不起作用。

More specifically, 进一步来说,

var $input = $form.find(".input").val();
var $input_units = $form.find(".input_units").val();
var $output_units = $form.find(".output_units").val();

These three lines work several times just after the script loads. 脚本加载后,这三行代码会运行几次。 But later on in the script, $output_units is undefined when the function is called. 但是在脚本的后面,调用函数时$ output_units是未定义的。 This one function and event handler are used to process several different individual HTML forms, which is why there are no specific IDs, only classes and $(this) based identifiers. 这个函数和事件处理程序用于处理几种不同的单独HTML表单,这就是为什么没有特定的ID,只有类和基于$(this)的标识符的原因。 But why would 2 of those commands work, but not the third? 但是,为什么其中两个命令可以工作,而第三个命令却不能工作呢? Why would it work sometimes, but not the rest of the time. 为什么有时会起作用,但其余时间却无法起作用。

Also, potentially BEFORE that even happens, there's this check: 另外,可能在发生这种情况之前,请执行以下检查:

if ( $form.attr("data-type") == "temp" ) 如果($ form.attr(“ data-type”)==“ temp”)

Most of the forms do not have this attribute. 大多数表单没有此属性。 And processing goes normally. 处理正常。 But halfway down the page, one form DOES have this attribute. 但是在页面的中间,一种形式具有此属性。 But IE9 still returns "undefined" from this. 但是IE9仍然从中返回“未定义”。 IE9 DOES support HTML5 data- attributes, so theres no obvious reason why this isn't working. IE9确实支持HTML5数据属性,因此没有明显的原因不能起作用。

Based on what I am currently logging to the console, you can see when those first three values are working properly, and when one of them cuts out and does not work. 根据我当前登录到控制台的内容,您可以看到前三个值何时正常运行,何时其中一个中断且不起作用。 And that same debug logging also shows you when IE9 misses the data-type="temp" attribute, because you end up with strings in your math, due to program flow not being redirected for this special case. 当IE9缺少data-type =“ temp”属性时,该调试日志也会向您显示,因为在这种特殊情况下,由于程序流未重定向,因此最终在数学运算中使用了字符串。

So random, perfectly valid commands seem to be failing, for no reason. 因此,随机,完全有效的命令似乎毫无理由地失败了。 But the behavior is 100% repeatable. 但是该行为是100%可重复的。 In this current state of things, I am getting no relevant errors in the JS console in IE9. 在目前的情况下,我在IE9的JS控制台中没有遇到任何相关错误。 It's like something in my code is causing IE9 to go haywire but I have no idea what it is. 就像我的代码中的某些内容导致IE9陷入困境,但我不知道它是什么。

The web page in question is here: http://www.whatsmyip.org/lib/unit-converter/ 有问题的网页在这里: http : //www.whatsmyip.org/lib/unit-converter/

The javascript file for that page is here: http://www.whatsmyip.org/js/tools/lib/unit-converter.js 该页面的javascript文件位于此处: http : //www.whatsmyip.org/js/tools/lib/unit-converter.js

Each converter on the page is it's own FORM tag, the "Temperature" form is the one with the data-type attribute that seems to be unreadable for IE9. 页面上的每个转换器都是其自己的FORM标记,“温度”形式是具有data-type属性的形式,对于IE9来说似乎是不可读的。 After it fails to read that, it seems that THEN it also starts being unable to read $form.find(".output_units").val(); 在无法读取该内容之后,似乎它也开始无法读取$ form.find(“。output_units”)。val();。 though it can read this fine before. 尽管它可以在以前阅读此书。

As much as I hate all IE, I'm not ready to abandon IE9 yet. 尽管我讨厌所有IE,但我还不准备放弃IE9。 I have lots of scripts on almost every page on my site, but this is the only issue I'm having. 我站点上几乎每个页面上都有很多脚本,但这是我唯一的问题。 All other pages work great. 所有其他页面都很好。 I do lots of beta testing in virtual machines. 我在虚拟机中进行了大量Beta测试。

P tags cannot contain block level items. P标签不能包含块级项目。 My original HTML contains lots of FORMs like this: 我的原始HTML包含许多这样的FORM:

<p>
<form>
      blahblah
      <div>Divider</div>
      dfasdfasdf
</form>

Even though FORM is a block tag, this code is valid because P's are self closing tags, so the FORM closes the P. However in IE9 specifically, this setup causes the FORM tag to go into what I will call "function-like-shit-mode", where any block item inside the FORM will close the FORM, and then the P. 即使FORM是一个块标记,该代码也是有效的,因为P是自闭标签,因此FORM会关闭P。 但是 ,特别是在IE9中,此设置会使FORM标记进入我称之为“函数似粪便”的位置。 -mode”,那么FORM内的任何块项都会关闭FORM,然后关闭P。

This bug, in my specific code, was causing half of my form elements to be rendered outside of the FORM. 在我的特定代码中,此错误导致一半表单元素呈现在FORM 之外 This was only clear when I started messing around with CSS of all things. 这只有在我开始弄乱所有CSS时才清楚。 Threw a border around my forms and look at that, only half of the form's elements are inside the border! 在表单周围画一个边框,然后看一下,表单中只有一半的元素在边框内!

The result of all of this, is that my Javascript couldn't find all of the values it needed, because I find SELECTs by searching for specific classes inside of FORMs. 所有这些的结果是,我的Javascript找不到所需的所有值,因为我通过搜索FORM内的特定类来找到SELECT。 Now the extra odd thing is that it would properly process most of the forms onLoad, but after that fail. 现在,更加奇怪的是,它可以正确处理大多数形式的onLoad,但是之后失败了。 As if the FORMs were rendering correctly for a second, then "snapping" back to the buggy version, excluding half of the items. 好像FORM可以正确渲染一秒钟,然后“捕捉”回有问题的版本,不包括一半的项目。

So in conclusion, I deleted the P tag in front of each of my FORM tags, and now IE9 is functionally totally normally. 因此,总而言之,我删除了每个FORM标记前面的P标记,现在IE9在功能上完全正常。 My FORMs can now contains DIVs without any issues. 我的表格现在可以包含DIV,而没有任何问题。

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

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