使用 jQuery 光谱颜色选择器调用 Ajax 函数时防止出错
Preventing Error when using jQuery Spectrum Color Picker to Call an Ajax function
我正在使用 jQuery Spectrum 并尝试在更改颜色时调用另一个函数。
另一个函数有一个 ajax 调用,但是,当它运行时,我得到这个错误:
spectrum.js?ver=1.0.7:1277 未捕获类型错误:无法读取未定义的 属性 'getBrightness'
我假设它与调用的同步性有关,但我不确定如何解决它。
jQuery('#mydiv').spectrum({
preferredFormat: "rgb",
showAlpha: true,
showPalette: true,
showInitial: true,
showSelectionPalette: true,
palette: ['rgba(0, 0, 0, 0)'],
showInput: true,
allowEmpty: true,
move: function(c) {
my_function(c);
},
change: function(c) {
my_function(c);
}
});
那么我调用的函数是这样的:
function my_function(color) {
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
datatype: 'JSON',
data: {
action: 'update_data',
color: color,
},
success: function(data) {
}
});
}
我能做些什么来使它正常工作吗?
这是一个也提供错误的 JSFiddle:
$(document).ready(function() {
$('#custom').spectrum({
preferredFormat: "rgb",
showAlpha: true,
showPalette: true,
showInitial: true,
showSelectionPalette: true,
palette: ['rgba(0, 0, 0, 0)'],
showInput: true,
allowEmpty: true,
change: function(color) {
console.log(color.toRgbString());
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.0.7/spectrum.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.0.7/spectrum.min.js"></script>
<input type='text' id="custom" />
这是一个工作示例,其中 ajax 调用为 jsfiddle:https://jsfiddle.net/bogatyr77/npekLc20/4/
我正在使用 jQuery Spectrum 并尝试在更改颜色时调用另一个函数。
另一个函数有一个 ajax 调用,但是,当它运行时,我得到这个错误:
spectrum.js?ver=1.0.7:1277 未捕获类型错误:无法读取未定义的 属性 'getBrightness'
我假设它与调用的同步性有关,但我不确定如何解决它。
jQuery('#mydiv').spectrum({
preferredFormat: "rgb",
showAlpha: true,
showPalette: true,
showInitial: true,
showSelectionPalette: true,
palette: ['rgba(0, 0, 0, 0)'],
showInput: true,
allowEmpty: true,
move: function(c) {
my_function(c);
},
change: function(c) {
my_function(c);
}
});
那么我调用的函数是这样的:
function my_function(color) {
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
datatype: 'JSON',
data: {
action: 'update_data',
color: color,
},
success: function(data) {
}
});
}
我能做些什么来使它正常工作吗?
这是一个也提供错误的 JSFiddle:
$(document).ready(function() {
$('#custom').spectrum({
preferredFormat: "rgb",
showAlpha: true,
showPalette: true,
showInitial: true,
showSelectionPalette: true,
palette: ['rgba(0, 0, 0, 0)'],
showInput: true,
allowEmpty: true,
change: function(color) {
console.log(color.toRgbString());
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.0.7/spectrum.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.0.7/spectrum.min.js"></script>
<input type='text' id="custom" />
这是一个工作示例,其中 ajax 调用为 jsfiddle:https://jsfiddle.net/bogatyr77/npekLc20/4/