为什么这个java-script函数会导致页面回发
本文关键字:java-script 函数 为什么 | 更新日期: 2023-09-27 18:13:01
我试图把签名字段在一个webform页面,使用这个演示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!";
}
表单中任何按钮的默认行为都是提交表单。简单的回答是:
clearButton.addEventListener("click", function (event) {
signaturePad.clear();
event.preventDefault(); // stop form submit.
});
更正确的答案是把按钮改成这样:
<input type="reset" name="reset" value="Clear" />
,那么你可以删除clearButton。adddeventlistener函数,并让重置按钮的默认行为清除表单。
这是因为<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">`