-4

我有一个调用 php 方法的 javascript 方法来获取数组“availabletags”中的一些数据。为了证明该函数是否被调用,我添加了

availableTags[0] = "Test";

当我把它放在我的服务器上尝试它时,什么也没有发生。所以我认为javascript函数没有被调用。

<link rel="stylesheet" href="js/demos.css">
<script type="text/javascript">
$(function() {
        var availableTags = new Array(400);
        availableTags[0] = "Test";
        availableTags = JSON.parse(<?php echo '"'.addslashes(include("/php/search_new.php")).'"';  ?>);
        for(var i=0;i<availableTags.length;i++){
            alert("<b>availableTags["+i+"] is  </b>=>"+availableTags[i]+"<br>");
            }
    }
</script>

</head>

<body>
        <input id="searchrecipes" type="text" name="searchrecipes" class="searchinput" style="margin-left: 850px; margin-top: 0px; width:170px; background: #fff url(images/search_icon.png) no-repeat 100%;" placeholder="Suchen..."></input>
        <input type="submit" name="buttonsenden" style="display:none;" value="" width: 5px></input>



</body>
</html>

有人知道我的错误吗?

4

1 回答 1

0

学习和使用 AJAX 来更改您的 javascript 数组,而无需刷新您的页面:AJAX 调用如下所示:

<script type='text/javascript'>
function yourFunction(str, typ)
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                myArray = xmlhttp.responseText; //here is the result
            }
        }
        xmlhttp.open("GET","myFile.php?param1="+str ,true); //here you lunch myFile 
        xmlhttp.send();
    }
</script>
于 2011-11-15T03:58:08.917 回答