默认值脚本在 IE7 中不起作用

发布于 2024-12-12 18:23:28 字数 1667 浏览 1 评论 0原文

我正在使用以下默认值脚本,该脚本适用于我测试过的所有浏览器(IE7 除外),其中“名称”字段不显示默认值(它应该显示“名称”)。

我在 IETester 中运行该页面,它给出了错误:“预期标识符、字符串或数字”在 'name[]': 'Name',}; 行上>。但我不知道如何修复这个错误。

编辑:删除'Name'后面的逗号后,此错误现已消失,但我仍然没有在 IE7 中看到默认值。您可以在此页面上了解我的意思。

有人可以帮忙解决这个问题吗?

谢谢,

尼克

<script>
$(function() {
    var defaults = {
        'name[]': 'Name',
    };

    // separating set and remove
    // note that you could add "defaults" as an arg if you had different
    // defaults for different fieldsets
    var setDefaults = function(inputElements) {
        $(inputElements).each(function() {
            var d = defaults[this.name];
            if (d) {
                // set with jQuery
                // we don't need the data - just check on the class
                $(this).val(d)
                    .addClass('default_value');
            }
        });
    };

    var removeDefaults = function(inputElements) {
        $(inputElements).each(function() {
           if ($(this).hasClass('default_value')) {
                $(this).val('')
                   .removeClass('default_value');
           }
        });
    };

    setDefaults(jQuery('form[name=booking] input'));

    // Toggles 
    $('form[name=booking]').delegate('input', {
        'focus': function() {
            removeDefaults($(this));
        },
        'blur': function() {
            // switch to using .val() for consistency
            if (!$(this).val()) setDefaults(this);
        }
    });
 }); 
 </script>

I am using the following default value script which is working in all browsers I have tested except IE7, where the default value isn't showing for the 'Name' field (It should show 'Name').

I ran the page in IETester and it gave the error: "Expected identifier, string or number" on the line with }; under 'name[]': 'Name',. I don't know how to fix this error though.

EDIT: This error has now gone, after removing the comma after 'Name' but I am still not seeing the default value in IE7. You can see what I mean on this page.

Could someone help with this?

Thanks,

Nick

<script>
$(function() {
    var defaults = {
        'name[]': 'Name',
    };

    // separating set and remove
    // note that you could add "defaults" as an arg if you had different
    // defaults for different fieldsets
    var setDefaults = function(inputElements) {
        $(inputElements).each(function() {
            var d = defaults[this.name];
            if (d) {
                // set with jQuery
                // we don't need the data - just check on the class
                $(this).val(d)
                    .addClass('default_value');
            }
        });
    };

    var removeDefaults = function(inputElements) {
        $(inputElements).each(function() {
           if ($(this).hasClass('default_value')) {
                $(this).val('')
                   .removeClass('default_value');
           }
        });
    };

    setDefaults(jQuery('form[name=booking] input'));

    // Toggles 
    $('form[name=booking]').delegate('input', {
        'focus': function() {
            removeDefaults($(this));
        },
        'blur': function() {
            // switch to using .val() for consistency
            if (!$(this).val()) setDefaults(this);
        }
    });
 }); 
 </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-12-19 18:23:28

并且它给出了错误:“预期标识符,字符串或数字”在带有 } 的行上;在“名称[]”下:“名称”,。但我不知道如何修复这个错误。

IE 在此处因尾随逗号而卡住:

var defaults = {
        'name[]': 'Name',
    };

IIRC,正确地按照 ECMAScript 标准。不管怎样,只要删除后面的逗号就可以了。

and it gave the error: "Expected identifier, string or number" on the line with }; under 'name[]': 'Name',. I don't know how to fix this error though.

IE chokes on the trailing comma here:

var defaults = {
        'name[]': 'Name',
    };

IIRC, rightfully as per the ECMAScript standard. Anyway, just remove the trailing comma and it will work.

放手` 2024-12-19 18:23:28

(我正在回答你的另一个问题,哈哈)

首先,你说逗号破坏了它,这是正确的 JSON 语法。

其次,不支持 IE 8 及以下版本(我想说根本不支持 IE,因为它很糟糕,但显然大多数互联网仍然使用它)

第三,你需要调试它才能弄清楚,看看解析 JSON(我认为是 $.parseJSON())。

我可以向您展示到底出了什么问题以及如何做,但是花一些时间通过在浏览器中打开控制台并使用 console.log(VARIABLE); 来练习调试 javascript 是非常值得的。在你的JS中。或者在不同时间使用警报(VARIABLE))。

例如

var setDefaults = function(inputElements) {
        $(inputElements).each(function() {
            var d = defaults[this.name];
            alert(d);
            if (d) {
                // set with jQuery
                // we don't need the data - just check on the class
                $(this).val(d)
                    .addClass('default_value');
            }
        });
    };

(I'm answering another question of yours lol)

Firstly you'r right in saying the comma broke it, that was incorrect JSON syntax.

Secondly, don't support IE 8 and below (I'm tempted to say don't support IE at all because it sucks, however apparently most of the internet still uses it)

Thirdly, you need to debug this to figure it out, have a look at Parsing JSON ($.parseJSON() i think).

I could show you exactly whats wrong and how to do it, but its more than worthwhile to spend some time practicing debugging javascript by opening up console in your browser and using console.log(VARIABLE); in your JS. Or using alert(VARIABLE) at different times).

eg

var setDefaults = function(inputElements) {
        $(inputElements).each(function() {
            var d = defaults[this.name];
            alert(d);
            if (d) {
                // set with jQuery
                // we don't need the data - just check on the class
                $(this).val(d)
                    .addClass('default_value');
            }
        });
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文