Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

html - Javascript function not called

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...