我试图为我的数据库列表中显示的每个项目创建按钮。目前我使用 Ajax 将列表显示到 PHP 文件中。
更多信息:目前此代码从 SQL 数据库中提取并列出所有问题。对于列表中的每个值,代码将其显示在“DOM”div 类中。我想为列表中的每个项目添加一个链接到advice.html 的按钮。
HTML:
<body>
<!--output of the json-->
<div>
<!--set the id to DOM to show output-->
<div id="DOM">
</div>
</div>
<a href="insert.html">insert</a>
<a href="delete.html">delete</a>
<a href="index.html">show data</a>
<a href="login.html">login</a>
<a href="register.html">register</a>
</body>
Javascript:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: "http://localhost/api/fetchdata.php",
type: "POST",
dataType: "json",
data: "param=no",
//on success it will call this function
success: function (data) {
var DOM = $('#DOM');
$.each(data, function (key, value) {
DOM.append("<h3>" + value.Subject + "</h3><p>" + value.Description + "</p>", $('<input type="button" href="localhost/advice.html" value="respond">')););
});
//if fail it will give this error
}, error: function (e) {
alert("failed to work");
}
});
});
</script>
我猜这条线
DOM.append("<h3>" + value.Subject + "</h3><p>" + value.Description + "</p>", $('<input type="button" href="localhost/advice.html" value="respond">')););
可以替换为
DOM.append("<h3>" + value.Subject + "</h3><p>" + value.Description + "</p>", $('<a href="localhost/advice.html"><button>respond</button></a>')););
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句