我想使用通过JS创建的OnClick按钮。我可以创建按钮,也可以将属性添加到Button上,但是Button不会启动我的功能,他只能重置网站。
$.ajax({
type: "POST",
url: "http://localhost/jQuery&PHPnew/Markt.N.php",
data:{status:status},
success: function(data){
var anzahl = data;
status = 1;
while (anzahl>nummer) {
nummer++;
$.ajax({
type: "POST",
url: "http://localhost/jQuery&PHPnew/Markt.N.php",
data:{nummer:nummer, status:status},
success: function(data){
var Daten = JSON.parse(data);
var Ausgabebereich = document.getElementById('main');
var f = document.createElement("form");
var bInhalt = document.createElement('button');
var Inhalt = document.createTextNode("Senden");
bInhalt.appendChild(Inhalt);
bInhalt.setAttribute("onclick", "myfunction()");
f.appendChild(bInhalt);
Ausgabebereich.appendChild(f);
}
})
}
}
})
function myfunction() {
alert("pls");
}
这是在浏览器中创建的代码
在myfunction()上,在警报之前添加行preventDefault()
,以防止重新加载页面(提交的默认行为-因为您的按钮位于表单上,因此警报将起作用。)>
否则,您可以在按钮上添加按钮类型,例如<button type="button">
,以确保该表单不会成为提交按钮