Javascript 为什么我会收到ReferenceError:框未定义错误
我声明了一个全局变量,并使用它从一个多选事件中捕获一个值。然而,我不明白为什么在模态窗口中会出现这个错误。有趣的是,另一个vars部门的地址工作得很好 在firebug控制台中,我可以看到正确显示的值。如果有人能指出我的错误,我将不胜感激。谢谢 声明变量框的代码Javascript 为什么我会收到ReferenceError:框未定义错误,javascript,jquery,Javascript,Jquery,我声明了一个全局变量,并使用它从一个多选事件中捕获一个值。然而,我不明白为什么在模态窗口中会出现这个错误。有趣的是,另一个vars部门的地址工作得很好 在firebug控制台中,我可以看到正确显示的值。如果有人能指出我的错误,我将不胜感激。谢谢 声明变量框的代码 <script> $(function () { var boxes; $('.switch-item').click(function (e) {
<script>
$(function () {
var boxes;
$('.switch-item').click(function (e) {
e.preventDefault();
var src = $(this).data('src');
var dst = $(this).data('dst');
var sel = $('#' + src + ' option:selected').detach();
$('#' + dst).append(sel);
$("#boxdest option:selected").prop("selected", false)
$('#srcBoxRslt').val('');
$('#srcBox').val('');
$('#counter').html( 'Total selected boxes for destruction: ' + $('#boxdest2 option').length );
$( "#submit" ).prop( "disabled", false );
boxes = $('#boxdest2').val();
console.log(boxes);
}); // end click
$('form').submit(function () {
if ($('#boxdest2').children().length == 0) {
notif({
type: "error",
msg: "<b>ERROR:<br /><br />You must enter some box(es) for destruction</b><p>Click anywhere to close</p>",
height: 99,
multiline: true,
position: "middle,center",
fade: true,
timeout: 3000
});
return false;
}
$('#boxdest2 option').prop({
selected: true
});
});
});
</script>
$(函数(){
var盒;
$('.switch item')。单击(函数(e){
e、 预防默认值();
var src=$(this.data('src');
var dst=$(this.data('dst');
var sel=$('#'+src+'选项:选定')。分离();
$('#'+dst).append(sel);
$(“#boxdest选项:选中”).prop(“选中”,false)
$('srcBoxRslt').val('');
$('srcBox').val('');
$('#counter').html('要销毁的选定框总数:'+$('#boxdest2选项').length);
$(“#提交”).prop(“已禁用”,错误);
box=$('#boxdest2').val();
控制台日志(框);
});//结束单击
$('form')。提交(函数(){
if($('#boxdest2').children().length==0){
诺蒂夫({
键入:“错误”,
msg:“错误:
您必须输入一些要销毁的框单击任意位置以关闭”,
身高:99,
多行:对,
位置:“中间,中间”,
是的,
超时:3000
});
返回false;
}
$(“#boxdest2选项”).prop({
所选:真
});
});
});
此代码中存在错误
<script type="text/javascript">
$(function () {
$('#destroy').click(function (e) {
$.Zebra_Dialog(
'Department: ' + depts +
'<br />' +
'Address: ' + address +
'<br />' +
'Boxes: ' + boxes <--- ERROR
,{
'type': 'confirmation',
'title': 'Destroy'
});
});
});
</script>
$(函数(){
$(“#销毁”)。单击(函数(e){
$.Zebra_对话框(
“部门:”+部门+
“
”+
'地址:'+地址+
“
”+
“框:”+框<代码>
var-box;//这是全局范围。
$(函数(){
var box;//不应在此处。这不是全局范围。这是函数的范围(在document.ready事件中传递)
});
我声明了一个全局变量
-这是您弄错的地方…变量框
是在第一个$(function(){…})中声明的;
范围在哪里定义了depts/address?一旦您进行了此更改,您可以进一步调试并使代码正常工作。干杯!非常感谢您的帮助
<script>
var boxes; // this is the global scope.
$(function () {
var boxes; // Should not be here. This not the global scope. This is the scope of the function ( which is passed on document.ready event )
});
</script>