【问题标题】:html2canvas call causes jQuery click to need two clicks for function to workhtml2canvas 调用导致 jQuery click 需要两次单击才能使功能正常工作
【发布时间】:2013-08-14 01:09:15
【问题描述】:

我已经阅读了所有相同问题但没有成功的问题。
我试过trigger()unbind('click')on('click', function())live('click')

我的问题是我必须点击两次保存按钮来验证并将数据提交到数据库并返回'success''fail'的消息以显示错误消息。

JQUERY

$('.save').click(function (event) {
    // GET CATEGORIA
    var category_change = 0;
    if (!$('#new_category').val()) {
        category = $('#categoria_id').val();
    } else {
        category = $('#new_category').val();
        category_change = 1;
    }

    // GET TITLE
    var title = $('input#title').val();

    // GET IMAGE
    var target = $('#parent');
    html2canvas(target, {
        onrendered: function (canvas) {
            image = canvas.toDataURL();
        }
    });

    // GET FORMATED CODE
    var array = ["n", "e", "s", "w"];
    var format = $('#parent').html();
    var strrep = '<div class="close">X</div>';
    var strrep2 = '<div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 1000;"></div>';

    var aux2 = 0;
    while (aux2 < 5) {
        $.each(array, function () {
            format = format.replace('<div class="ui-resizable-handle ui-resizable-' + this + '" style="z-index: 1000;"></div>', '');
        });
        format = format.replace(strrep, '');
        format = format.replace(strrep2, '');
        aux2++;
    }
    var code = '<div id="parent">' + format + '</div>';


    if (title != '' && $('.child').length != 0) {
        $('.db_result').html('<img src="images/loader.gif" />');
        $.post("db_prox.php", {
            category: category,
            title: title,
            image: image,
            code: code,
            category_change: category_change
        }, function (post_result) {
            alert('2');
            if (post_result == 'success') {
                $('.db_result').text('O template foi guardado com sucesso!');
                $('.continue').prop("disabled", false);
            } else {
                $('.db_result').text('Ocorreu um erro na gravação do template!');
            }
        });
    }

    // INSERIR NA BASE DE DADOS CLOSE
});

PHP db_prox.php

<?php
require_once 'includes/functions.php';

$categoria = $_POST['category'];
$title = $_POST['title'];
$image = $_POST['image'];
$code = $_POST['code'];
$nova_categoria = $_POST['category_change'];

if($nova_categoria == 1){

    $result = get_cat_last_created();
    $cat_id = mysqli_fetch_assoc($result);
    $aux_id = $cat_id['categoria_id'] + 1;
    $query = "INSERT INTO categorias (categoria_id, name) VALUES ('$aux_id', '$categoria')";
    if(mysqli_query($connection, $query)) {
        $query2 = "INSERT INTO templates (categoria_id, title, image, code) VALUES ('$aux_id', '$title', '$image', '$code')";

        if(mysqli_query($connection, $query2)) {
            echo "success";
        }
        else {
            echo "fail";
        }
    }
}else{
    $query = "INSERT INTO templates (categoria_id, title, image, code) VALUES ('$categoria', '$title', '$image', '$code')";
    if(mysqli_query($connection, $query)) {
        echo "success";
    }
    else {
        echo "fail";
    }
}
mysqli_close($connection);
?>

【问题讨论】:

  • 您为什么不尝试将代码减少到最低限度的示例,看看会发生什么?
  • 是的,如果我这样做,它会让我单击一次执行。但我需要在单击保存按钮后执行所有 GET。
  • 如果您担心代码的安全性,sql 注入几乎是可以预测的
  • 如何避免上述代码中的sql注入?
  • @circle73 这需要时间,但它最终会比等待其他人弄清楚它更快:将你的代码减少到可行的程度,然后一次添加其余的步骤。

标签: jquery click html2canvas


【解决方案1】:

onrendered 函数是异步的

 html2canvas(target, {
    onrendered: function (canvas) {
        image = canvas.toDataURL();
    }
});

意味着在此之后同步运行的任何代码都没有分配 image 变量。

onrendered 函数内移动您的 ajax 帖子将使其在创建图像后发送请求,即:

 html2canvas(target, {
    onrendered: function (canvas) {
        image = canvas.toDataURL();
        $.post("db_prox.php", {
          category: category,
          title: title,
          image: image,
          code: code,
          category_change: category_change
        }, function (post_result) {
          alert('2');
          if (post_result == 'success') {
            $('.db_result').text('O template foi guardado com sucesso!');
            $('.continue').prop("disabled", false);
          } else {
            $('.db_result').text('Ocorreu um erro na gravação do template!');
          }
        });
    }
});

【讨论】:

  • 嗨尼克拉斯!你在这里有很棒的插件。我正要试着联系你。该解决方案对上述问题非常有效。但现在我有另一个问题。我不知道这个插件在图像 jpeg 中输出的大小是否有限制,但是现在当我运行它时,所有图像都会渲染到 div #parent 的一半。有没有办法扩展大小限制,以便图像整体输出?
  • @circle73 默认情况下,toDataURL 返回一个 png 图像。 div #parent 是否设置了溢出滚动?
  • 我正在使用这种方法将图像返回为 jpeg image = canvas.toDataURL("image/jpeg"); 这是 #parent 的 css :#parent{ width: 800px; height: 600px; position: absolute; top: 0; left: 0; border: solid 2px gray; padding: 0; overflow: scroll; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
相关资源
最近更新 更多