再次调用时,JS函数不会创建另一个qr代码对象

时间:2017-10-26 19:39:49

标签: javascript c# jquery qr-code

我正在使用名为qrcode.js(https://github.com/davidshimjs/qrcodejs)的javascript文件。

我要做的是从用户那里获取输入,然后从我的c#代码后面调用javascript函数(createQRCode)来创建多个QR码。

protected void SubmitButton_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction", "createQRCode('tr1c1', 'www.google.com')", true);
    tr1c1Label.Text = "www.google.com";
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction", "createQRCode('tr1c2', 'www.reddit.com')", true);
    tr1c2Label.Text = "www.reddit.com";
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction", "createQRCode('tr1c3', 'www.stackoverflow.com')", true);
    tr1c3Label.Text = "www.stackoverflow.com";
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction", "createQRCode('tr2c1', 'www.twitter.com')", true);
    tr2c1Label.Text = "www.twitter.com";
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction", "createQRCode('tr2c2', 'www.facebook.com')", true);
    tr2c2Label.Text = "www.facebook.com";
    ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction", "createQRCode('tr3c1', 'www.myspace.com')", true);
    tr3c1Label.Text = "www.myspace.com";
}

点击提交按钮后,它会多次调用此javascript函数:

function createQRCode(div, url) {
        var qrcode = new QRCode(document.getElementById(div), {
            text: url,
            width: 128,
            height: 128,
            colorDark: "#000000",
            colorLight: "#ffffff",
            correctLevel: QRCode.CorrectLevel.H
        });
    };

后面的代码传递一个div来创建qr代码和一个url来放入qr代码。这只能工作一次。它每次调用javascript函数,但不生成新的qr代码。它只创建第一个并且它是正确的。 这是我的意思的截图: QR Code Creation

我的问题是每次调用函数时如何让javascript创建新的QR码?

1 个答案:

答案 0 :(得分:1)

我不知道c#,但我认为错误在

ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction", "createQRCode('tr1c2', 'www.reddit.com')", true);

标识符(第三个参数)必须对每个脚本都是唯一的! 试试这个:

protected void SubmitButton_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction1", "createQRCode('tr1c1', 'www.google.com')", true);
tr1c1Label.Text = "www.google.com";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction2", "createQRCode('tr1c2', 'www.reddit.com')", true);
tr1c2Label.Text = "www.reddit.com";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction3", "createQRCode('tr1c3', 'www.stackoverflow.com')", true);
tr1c3Label.Text = "www.stackoverflow.com";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction4", "createQRCode('tr2c1', 'www.twitter.com')", true);
tr2c1Label.Text = "www.twitter.com";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction5", "createQRCode('tr2c2', 'www.facebook.com')", true);
tr2c2Label.Text = "www.facebook.com";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallMyFunction6", "createQRCode('tr3c1', 'www.myspace.com')", true);
tr3c1Label.Text = "www.myspace.com";

}