如何从搜索按钮触发AJAX

时间:2022-08-23 22:32:34

I have the following Javascript:

我有以下Javascript:

<script>
function showtickets(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "/processes/editticket.php?WTNum=" + str, true);
    xmlhttp.send();
}

It works perfectly if I use a selectbox and trigger the function with "onchange". However, my client requested a searchbox instead. I cannot get it to work with a search box. Here is my HTML for the search box and the div where the results should go.

如果我使用选择框并使用“onchange”触发该功能,它将完美运行。但是,我的客户请求搜索框。我无法使用搜索框。这是我的搜索框的HTML和结果应该去的div。

    <form class='form-horizontal col-xs-12 col-md-12 col-lg-12' name="editticket" method="post" action="/processes/updateticket.php">
   <div class='panel panel-primary'>
        <div class='panel-heading'>
            <h3 class='panel-title'>Edit Work Tickets</h3>
        </div>

            <div class='panel-body'>
                <div class="input-group">

    <span class="input-group-addon">Enter Ticket #</span>
                            <input type="text" class="form-control" id="search" name="WTNum" >
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button" onclick="showtickets(document.getElementById('search').value)">Search</button>
                            </span>

                 <div id="txtHint"><b></b></div>

</form>

Am I missing something!! This is driving me INSANE!! Thank you all in advance. It is greatly appreciated.

我错过了什么!这让我开始了!谢谢大家。非常感谢。

2 个解决方案

#1


1  

Why not just call the showtickets function without arguments and then inside the function you do this

为什么不在没有参数的情况下调用showtickets函数,然后在函数内部执行此操作

str = document.getElementById('search').value;

#2


0  

I think you must you javascript debug or alert in some scope to find where error

我认为你必须在某些范围内进行javascript调试或警报,以找到错误的位置

#1


1  

Why not just call the showtickets function without arguments and then inside the function you do this

为什么不在没有参数的情况下调用showtickets函数,然后在函数内部执行此操作

str = document.getElementById('search').value;

#2


0  

I think you must you javascript debug or alert in some scope to find where error

我认为你必须在某些范围内进行javascript调试或警报,以找到错误的位置