4

2012 年 7 月 17 日更新:不仅仅是“current_width”和“current_height”字段被清除 - 它是整个表单。从视觉上看,浏览器中的表单仍然包含值,但是 jQuery “阻塞”或其他东西 - 并且突然开始将表单中的所有字段读取为 null。

仅使用 .val() 不是问题,因为我已经通过在 preview() 函数中使用 .children() 和 .each() 对其进行了验证:

var debugstring = '';
var debugform = $('#hidden_thumb_fields').children("input").each(function (){
    var child = $(this);
    debugstring += child.attr('id')+':'+child.val()+',';
});

console.log(debugstring);

和以前一样,它可以在您拖动选择区域时正常工作,但很快它就会开始返回 null。

请注意,这只发生在 Chrome 中。


原始帖子:(编辑:删除了 mlx 日志输出)

设想:

  1. 上传的图像宽度和高度写入 2 个表单字段:current_width 和 current_height
  2. 在上传的图像上启用 imgAreaSelect
  3. imgAreaSelect 的 onInit 和 onSelectChange 调用函数“预览”
  4. “预览”使用 .val() 读取宽度和高度表单字段的值
  5. 当您移动选择区域时,会重复调用“预览”
  6. 最终,.val() 开始返回 NULL,即使字段仍然明显包含正确的值并且没有被修改。这只发生在 Chrome 中。Firefox 和 Safari 都运行良好。IE 没有经过测试。
  7. 如果我使用纯 Javascript (document.getElementById().value),则没有任何问题。

正如经常引用的那样,我在 $(document).ready 之后调用 imgAreaSelect。之前唯一要做的就是设置 3 个变量,我已经将其包含在下面的预览示例中。

附带说明一下,如果有更好或更可接受的方法来获取图像宽度和高度数据,我会全力以赴。图片上传脚本 (PHP) 是首先设置这些字段的原因,以我的经验水平,我不确定是否有更好的方法来做到这一点。我想要图像的真实尺寸,而不是 .css 宽度和高度,因为它们可能(在我的场景中,通常是)不同。

问题似乎起源的特定代码位就在预览功能的开头:

function preview(img, selection) { 
    var current_width = $('#thumbnail_form').find('#current_width').val();
    var current_height = $('#thumbnail_form').find('#current_height').val();

这是预览()。如果我删除 .find() 并直接使用 $('#current_width').val(),问题仍然存在:

var thumb_width=128;
var thumb_height=128;
var counter=1;

function preview(img, selection) { 
    var current_width = $('#thumbnail_form').find('#current_width').val();
    var current_height = $('#thumbnail_form').find('#current_height').val();

    //var current_width = document.getElementById("current_width").value;
    //var current_height = document.getElementById("current_height").value;

    var scaleX = thumb_width / selection.width; 
    var scaleY = thumb_height / selection.height; 

    var img_css_width = Math.round(scaleX * current_width) + 'px';
    var img_css_height = Math.round(scaleY * current_height) + 'px';
    var img_css_margin_left = '-' + Math.round(scaleX * selection.x1) + 'px';
    var img_css_margin_top = '-' + Math.round(scaleY * selection.y1) + 'px';

    // mlx and the css fields are just to help diagnose the problem
    var mlx = document.getElementById('mlx').value;
    if (!current_width) {
        current_width = 'NULL';
    }
    if (!current_height) {
        current_height = 'NULL';
    }
    $('#mlx').val(mlx+counter+")w:"+current_width+",h:"+current_height+"\n");
    $('#img_css_width').val(img_css_width);
    $('#img_css_height').val(img_css_height);
    $('#img_css_margin_left').val(img_css_margin_left);
    $('#img_css_margin_top').val(img_css_margin_top);

    $('#displayed_icon_container').find('#displayed_icon').css({
    //$('#displayed_icon').css({
        width:  img_css_width,
        height: img_css_height,
        marginLeft: img_css_margin_left,
        marginTop: img_css_margin_top 
    });
    $('#x1').val(selection.x1);
    $('#y1').val(selection.y1);
    $('#x2').val(selection.x2);
    $('#y2').val(selection.y2);
    $('#w').val(selection.width);
    $('#h').val(selection.height);

    // if I re-write these values back to the form, I can circumvent the issue, but why is this necessary?      
    //$('#current_width').val(current_width);
    //$('#current_height').val(current_height);
    $('#sx').val(scaleX);
    $('#sy').val(scaleY);
    counter++;
}

这是 HTML 的相关字段。我不相信文件的其余部分是相关的,但如果有要求,我很乐意发布更多内容。

<div id='thumbnail_form'>
<form name='form' action='' method='post'>
<div id='hidden_thumb_fields'>
x1: <input type='text' name='x1' value='' id='x1' /><br />
y1: <input type='text' name='y1' value='' id='y1' /><br />
x2: <input type='text' name='x2' value='' id='x2' /><br />
y2: <input type='text' name='y2' value='' id='y2' /><br />
Selection Width (w): <input type='text' name='w' value='' id='w' /><br />
Selection Height (h): <input type='text' name='h' value='' id='h' /><br />

Old Icon Display URL: <input type='text' name='old_icon_display_URL' value='/whatever/thing.png' id='old_icon_display_URL' /><br />

Large File Width (current_width): <input type='text' name='current_width' value='' id='current_width' /><br />
Large File Height (current_height): <input type='text' name='current_height' value='' id='current_height' /><br />
Img Display Width: <input type='text' name='img_width' value='' id='img_width' /><br />
Img Display Height: <input type='text' name='img_height' value='' id='img_height' /><br />

Img CSS Width: <input type='text' name='img_css_width' value='' id='img_css_width' /><br />
Img CSS Height: <input type='text' name='img_css_height' value='' id='img_css_height' /><br />
Img CSS Margin Left: <input type='text' name='img_css_margin_left' value='' id='img_css_margin_left' /><br />
Img CSS Margin Top: <input type='text' name='img_css_margin_top' value='' id='img_css_margin_top' /><br />

scalex: <input type='text' name='sx' value='' id='sx' /><br />
scaley: <input type='text' name='sy' value='' id='sy' /><br />
Math Log X: <textarea name='mlx' value='' id='mlx' cols='40' rows='10'></textarea><br />

Icon Basedir: <input type='text' name='icon_basedir' value='' id='icon_basedir' /><br />
Icon BaseURL: <input type='text' name='icon_baseURL' value='' id='icon_baseURL' /><br />
Icon Relpath: <input type='text' name='icon_relpath' value='' id='icon_relpath' /><br />
Large Filename: <input type='text' name='large_image_filename' value='' id='large_image_filename' /><br />
Thumb Filename: <input type='text' name='thumb_image_filename' value='' id='thumb_image_filename' /><br />
</div>

<input type='button' name='cancel_thumb' value='Cancel' id='cancel_thumb' />
<input type='submit' name='save_thumb' value='Crop Image' id='save_thumb' />
</form>
</div>
4

3 回答 3

0

这个问题是由Chrome 的 Firebug Lite 扩展引起的。无论 Firebug Lite 面板是否打开,以及我是否将输出记录到控制台,都会出现问题。现在我知道在隐身模式下测试东西了。感谢大家的时间,并为误报道歉......

于 2012-07-17T21:20:38.043 回答
0

抛出 console.log(current_height,current_width); 就在它们设置好之后。我的猜测是它们返回 null,因为您正在通过 Id 检查在 dom 中的多个位置找到的元素

于 2012-07-13T17:46:40.027 回答
0

好的,也许我不明白你的问题,如果我错了,请纠正我:

现在,您发布的代码在其他代码允许“预览”选择器在屏幕上移动之后开始调用。

当调用此代码时,它似乎开始在两个字段中看到 null,这些字段在调用此代码之前由其他 javascript 填充。

这个对吗?

如果是这样,那么问题可能出在填充这些字段的其他代码中。除了下面的注释之外,我看不到此代码有任何明显的问题,更仔细地阅读您的代码显示的只是您为尝试调试问题而输入的代码。


也许这会导致溢出:

$('#mlx').val(mlx+counter+")w:"+current_width+",h:"+current_height+"\n");

更改此行时会发生什么:

<textarea name='mlx' value='' id='mlx' cols='40' rows='10'></textarea><br />

对此:

<textarea name='mlx' value='' id='mlx' cols='40' rows='200'></textarea><br />

因为 26 * 14 大约等于 40 * 10。

于 2012-07-13T17:50:30.233 回答