jQuery:Javascript抛出错误“操作不安全”设定值时 [英] jQuery: Javascript throws Error "The operation is insecure" when setting value

查看:125
本文介绍了jQuery:Javascript抛出错误“操作不安全”设定值时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为不解释输入元素的占位符属性的浏览器找到后备解决方案。
我有这个简单的jQuery脚本但它抛出一个错误

I am trying to find a fallback solution for browsers who do not interpret the placeholder attribute for input elements. I have this simple jQuery Script but it throws an Error

SecurityError: "The operation is insecure.
this.value = val;"

这是我的剧本:

$('document').ready(function(){
       $('input').each(function() {
           if ($(this).val() === '' || $(this).val() === undefined) {
               $(this).val($(this).attr('placeholder'));
           }
       });
});

任何想法我能做什么?或者我做错了什么?或者这个错误意味着什么?
它发生在Firefox中,还没有在其他浏览器中测试过它。

Anyone any ideas what i can do? Or what i am doing wrong? Or what this error means? It happens in Firefox, haven't tested it in other Browsers yet.

推荐答案

我刚刚修了一个类似的我的项目中的问题。事实证明,我试图设置< input type =file...> 输入的值。您可能面临同样的问题,因为您正在选择文档的所有输入而不管其类型。

I have just fixed a similar problem in my project. It turned out that I was trying to set a value of a <input type="file" ...> input. It seems that you may be facing the same problem, because you are selecting all inputs of a document regardless of their type.

如果您安装了firebug,请尝试查找在尝试修改输入值之前插入 log 命令导致此错误的输入。

If you have firebug installed, try looking for the input that causes this error by inserting a log command before trying to modify the input's value.

$('document').ready(function(){
       $('input').each(function() {
           if ($(this).val() === '' || $(this).val() === undefined) {

               // Log goes here
               window.console.log(
                   'There is an input without a value!', 
                   this);

               $(this).val($(this).attr('placeholder'));
           }
       });
});

这篇关于jQuery:Javascript抛出错误“操作不安全”设定值时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆