将DB记录附加到按钮中

时间:2016-02-05 09:48:51

标签: javascript jquery append

我正在尝试将检索到的DB记录附加到按钮中,我可以点击这些按钮转到下一页。

 function mywall(){
        $("#wallcontentset").empty();
        var xmlhttp = new XMLHttpRequest();
         var url = serverURL() + "/category.php";

         xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          mywallresult(xmlhttp.responseText);
          };
         }

         xmlhttp.open("GET", url, true);
         xmlhttp.send();
        }

        function mywallresult(response) {
        var arr = JSON.parse(response);
        var i;
        $("#wallcontentset").empty();
        for(i = 0; i < arr.length; i++) {

         var a = document.createElement("a");
         a.setAttribute("href", "#");            
         a.setAttribute("onclick","listitembycategory.html?categoryid=" + arr[i].categoryid);                 
         a.setAttribute("class","ui-btn");          
         a.innerHTML = arr[i].categoryname.toString();
         $("#wallcontentset").append(a);

        }

    }

以上是我编写并放在脚本中的一组函数。函数mywall()工作正常,它检索数据库中的每条记录。 但是我的函数mywallresult()有一些问题。

它为每个检索到的记录创建一个按钮,但是单击该按钮时不会链接到下一页。我无法确定我的a.setAttribute是什么问题。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

你只需要替换它:

 a.setAttribute("href", "#");            
 a.setAttribute("onclick","listitembycategory.html?categoryid=" + arr[i].categoryid);

由此:

a.setAttribute("href", "listitembycategory.html?categoryid=" + arr[i].categoryid);