onkeyup 函数只触发一次 [英] onkeyup function only firing once

查看:23
本文介绍了onkeyup 函数只触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 onkeyup 触发多次,但它似乎只触发一次!

I need the onkeyup to fire more than once, but it seems to be only firing once!

当我在输入框中输入内容时,它会进行搜索,但是每当我退格并搜索其他内容时,div 保持不变..

When I enter something into the input box, it searches, but then whenever I backspace and search something else, the div stay's the same..

这是我的代码:

<script type="text/javascript">
function suggest1() {
    var dam_text = document.getElementById('dam').value;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject('MicrosoftXMLHTTP');
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('myDiv').innerHTML = xmlhttp.responseText;
        }
    }
    var target = 'dam_search.php?dam_text=' + dam_text;
    xmlhttp.open('GET', target, true);
    xmlhttp.send();
}
</script>

<input type="text" name="dam" id="dam" onkeyup="suggest1();"><br />
<div id="myDiv"></div>

这里是 dam_search.php

Here is dam_search.php

<?php

//connect to db stuff here

if (isset($_GET['dam_text'])) {
    $dam = $_GET['dam_text'];
    getSuggest($text);
}

function getSuggest($text) {

    $sqlCommand = "SELECT `name` FROM `table1` WHERE `name` LIKE '%$dam_text%'";

    $query = mysql_query($sqlCommand);

    $result_count = mysql_num_rows($query);

    while ($row = mysql_fetch_assoc($query)) {
        echo $row['name'].'<br />';
    }

}
?>

另外:我想知道如何将搜索到的名称的返回放入输入框的下拉列表中,而不是放入 div 中,因此当我单击其中一个名称时,它会自动填充输入框.

ALSO: I am wondering how I can put the return of the name's it has searched into a dropdown from the input box instead of into the div, so when I click on one of the names, it auto fills the input box.

谢谢!

推荐答案

发现我的问题.查询未正确处理!

Found out my problem. The query wasn't correctly being processed!

我将变量 $dam_text 作为 LIKE 语句,而它应该是 $dam:

I had the variable $dam_text as the LIKE statement, when it should have been $dam:

<?php

//connect to db stuff here

if (isset($_GET['dam_text'])) {
    $dam = $_GET['dam_text'];
    getSuggest($text);
}

function getSuggest($text) {

    $sqlCommand = "SELECT `name` FROM `table1` WHERE `name` LIKE '%$dam_text%'";

    $query = mysql_query($sqlCommand);

    $result_count = mysql_num_rows($query);

    while ($row = mysql_fetch_assoc($query)) {
        echo $row['name'].'<br />';
    }

}
?>

另外,变量 $dam 没有在函数内部提交,所以我将它从 'if' 语句移到函数中:

Also, the variable $dam wasn't being submitted inide the function, so I moved it from the 'if' statement, into the function:

<?php

//connect to db stuff here

if (isset($_GET['dam_text'])) {
    getSuggest($text);
}

function getSuggest($text) {

    $dam = $_GET['dam_text'];

    $sqlCommand = "SELECT `name` FROM `table1` WHERE `name` LIKE '%$dam%'";

    $query = mysql_query($sqlCommand);

    $result_count = mysql_num_rows($query);

    while ($row = mysql_fetch_assoc($query)) {
        echo $row['name'].'<br />';
    }

}
?>

以上代码完美运行!原来它毕竟不是onkeyup!感谢您的帮助!

The above code works perfectly! Turns out it wasn't onkeyup after all! Thanks for all your help!

这篇关于onkeyup 函数只触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆