jQuery:Javascript 在设置值时抛出错误“操作不安全”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11780959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Javascript throws Error "The operation is insecure" when setting value
提问by Andresch Serj
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
我正在尝试为不解释输入元素的占位符属性的浏览器找到后备解决方案。我有这个简单的 jQuery 脚本,但它抛出一个错误
SecurityError: "The operation is insecure.
this.value = val;"
Here's my script:
这是我的脚本:
$('document').ready(function(){
$('input').each(function() {
if ($(this).val() === '' ||?$(this).val() === undefined) {
$(this).val($(this).attr('placeholder'));
}
});
});
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.
任何人有什么想法我能做什么?或者我做错了什么?或者这个错误是什么意思?它发生在 Firefox 中,尚未在其他浏览器中测试过。
回答by Igor Zinov'yev
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.
我刚刚在我的项目中修复了一个类似的问题。结果是我试图设置一个<input type="file" ...>
输入的值。您似乎面临着同样的问题,因为您正在选择文档的所有输入,而不管它们的类型。
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.
如果您安装了 firebug,请尝试通过log
在尝试修改输入值之前插入命令来查找导致此错误的输入。
$('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'));
}
});
});
回答by Kai
I had an insecure warning in conjunction with antoher function. The reason there simply was, that a library function was called with an array given as parameter, but it expected an element (dom). The result was the expected, but I'm sure, this won't be in any case. So check if the types of your variables are the one you (or the other side) want's it to.
我有一个不安全的警告与另一个功能一起使用。原因很简单,即使用给定的数组作为参数调用了一个库函数,但它需要一个元素(dom)。结果是预期的,但我敢肯定,无论如何都不会。因此,请检查变量的类型是否是您(或另一方)想要的类型。