Javascript function not called

2019-10-07 23:05发布

I´ve got a javascript method that calls a php method to get some data in the array "availabletags". To prove if the function is even called, I added

availableTags[0] = "Test";

When I put it on my server to try it, nothings happens. So I think the javascript function isn´t called.

<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>

Does anybody know my mistake?

1条回答
Explosion°爆炸
2楼-- · 2019-10-07 23:30

Learn and use AJAX to change your javascript array without to refresh your page: AJAX call looks like:

<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>
查看更多
登录 后发表回答