我正在尝试使用此演示版http://szimek.github.io/signature_pad和https://github.com/szimek/signature_pad处的代码将签名字段放入网页表单页面。
为什么当我点击清除按钮整个页面重新加载(发生回发)? 这不是客户端电话吗? 我该如何预防?
aspx html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8">
<title>Signature Pad demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<meta name="description" content="Signature Pad - HTML5 canvas based smooth signature drawing using variable width spline interpolation.">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" href="css/signature-pad.css">
</head>
<body>
<form id="form1" runat="server">
<div id="signature-pad" class="m-signature-pad">
<div class="m-signature-pad--body">
<canvas></canvas>
</div>
<div class="m-signature-pad--footer">
<div class="description">
Sign above</div>
<button class="button clear" data-action="clear">
Clear</button>
<asp:Button Text="Save" runat="server" ID="btnSave" CssClass="button save" data-action="save"
OnClick="Save" />
</div>
<script src="js/signature_pad.js"></script>
<script src="js/app3.js"></script>
<input type="hidden" id="hfSign" runat="server" />
</div>
</form>
</body>
</html>
app3.js:
var wrapper = document.getElementById("signature-pad"),
clearButton = wrapper.querySelector("[data-action=clear]"),
saveButton = wrapper.querySelector("[data-action=save]"),
canvas = wrapper.querySelector("canvas"),
signaturePad;
// Adjust canvas coordinate space taking into account pixel ratio,
// to make it look crisp on mobile devices.
// This also causes canvas to be cleared.
function resizeCanvas() {
// When zoomed out to less than 100%, for some very strange reason,
// some browsers report devicePixelRatio as less than 1
// and only part of the canvas is cleared then.
var ratio = Math.max(window.devicePixelRatio || 1, 1);
canvas.width = canvas.offsetWidth * ratio;
canvas.height = canvas.offsetHeight * ratio;
canvas.getContext("2d").scale(ratio, ratio);
}
window.onresize = resizeCanvas;
resizeCanvas();
signaturePad = new SignaturePad(canvas);
clearButton.addEventListener("click", function (event) {
signaturePad.clear();
});
saveButton.addEventListener("click", function (event) {
if (signaturePad.isEmpty()) {
alert("Please provide signature first.");
} else {
document.getElementById("hfSign").value = signaturePad.toDataURL();
}
});
代码背后的C#:
protected void Save(object sender, EventArgs e)
{
string signature = hfSign.Value;
if (!String.IsNullOrEmpty(signature))
btnSave.Text = "Signature Saved!";
}
答案 0 :(得分:2)
表单中任何按钮的默认行为是提交表单。答案很简单:
clearButton.addEventListener("click", function (event) {
signaturePad.clear();
event.preventDefault(); // stop form submit.
});
更正确的答案是将按钮更改为:
<input type="reset" name="reset" value="Clear" />
然后你可以删除clearButton.addEventListener函数并让重置按钮的默认行为清除表单。
答案 1 :(得分:1)
这是因为<button>
元素默认为<button type="submit">
,所以它实际上是在提交表单。
为防止这种情况,您可以替换
`<button class="button clear" data-action="clear">`
与
`<button type="button" class="button clear" data-action="clear">`
默认情况下不执行任何操作,或
`<button type="reset" class="button clear" data-action="clear">`
默认情况下会清除表单。