单击结果后,如何在实时ajax搜索中清除输入字段空白

时间:2017-06-05 16:52:02

标签: javascript jquery ajax

我有一个使用PHP jQuery和ajax的简单的ajax实时搜索脚本。这一切都很好,但输入框没有完全清除,所以结果框不会消失,除非我手动清除它..

JS -

@echo off

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
set datetimefile=%datetimefile:~0,8%_%datetimefile:~8,6%
set "logfile=%logdirectory:~0,-1%\Bcopy%datetimefile%.log^""
set "logxcpy=%logdirectory:~0,-1%\Xcopy%datetimefile%.log^""

set sourcedirectory="d:\myDir"
set targetdirectory="w:\myDir"
set logdirectory="W:"
set whereiszip="C:\Program Files\7-Zip\7z.exe"

echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% START   -----
echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% START   ----- >> %logfile% 2>&1

for /f "tokens=*" %%A in ('xcopy %sourcedirectory% %targetdirectory% /f /i /s /h /d /y /l') do (call :subroutine "%%A")
xcopy %sourcedirectory% %targetdirectory% /f /i /s /h /d /y >> %logxcpy% 2>&1

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% FINISH   -----
echo -----   BACKUP FROM: %sourcedirectory% TO: %targetdirectory% AT: %datetimecopy% LOG: %logfile% ZIP: %whereiszip% FINISH   ----- >> %logfile% 2>&1

if "%1"=="" goto :finish

if "%1"=="S" shutdown /s
if "%1"=="M" goto :outlook
if "%1"=="O" goto :outlook


:subroutine
if "%~1"=="" goto :cannotparse
set str1=%1
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetimefile=%%I
set datetimecopy=%datetimefile:~0,8% %datetimefile:~8,2%:%datetimefile:~10,2%:%datetimefile:~12,2%
if not defined str1 goto :cannotparse
if x%str1: -> =%==x%str1% goto :cannotparse
echo string %str1% contains -^>
set str=%1
@setlocal enableextensions enabledelayedexpansion
set "find= -> "
call set last=%%str:*!find!=%%
call set first=%%str:!last!=%%
call set first=%%first:!find!=%%
call set first=!first!^"
call set last=^"!last!
set nqlast=!last:~1,-1!
@setlocal enableextensions disabledelayedexpansion
for %%A in ("%nqlast%") do (
    call set folder=%%~dpA
    call set file=%%~nxA
)
echo %datetimecopy% INPUT: [%str%] source file: [%first%] destination file: [%last%] path: [%folder%] name: [%file%]
echo %datetimecopy% INPUT: [%str%] source file: [%first%] destination file: [%last%] path: [%folder%] name: [%file%] >> %logfile% 2>&1
if NOT EXIST %last% GOTO :cpyonly
set "g=%last%"
call set "newg=%%g:\=\\%%"
for /f "tokens=2 delims==" %%N in ('wmic datafile where name^=%%newg%% get LastModified /format:list') do set lastmod=%%N
set "target="%file%.B%lastmod:~0,14%^""
echo %datetimecopy% RENAME rename %last% %target%
echo %datetimecopy% RENAME rename %last% %target% >> %logfile% 2>&1
rename %last% %target% >> %logfile% 2>&1
set "target="%folder%%file%.B%lastmod:~0,14%^""
set "ztarget="%folder%%file%.B%lastmod:~0,14%.zip^""
if EXIST %whereiszip% (
    echo %datetimecopy% 7ZIP: %whereiszip% a -tzip -mx9 %ztarget% %target%
    echo %datetimecopy% 7ZIP: %whereiszip% a -tzip -mx9 %ztarget% %target% >> %logfile% 2>&1
    %whereiszip% a -tzip -mx9 %ztarget% %target% >> %logfile% 2>&1
    if %ERRORLEVEL% EQU 0 (
        echo %datetimecopy% DELETE: del %target%
        echo %datetimecopy% DELETE: del %target% >> %logfile% 2>&1
        del %target% >> %logfile% 2>&1
    )
)
goto :cpyonly
endlocal
goto :finish

:cpyonly
set "quotedfolder="%folder%^""
echo %datetimecopy% XCOPY: xcopy %first% %quotedfolder% /f /i /s /h /d /y
echo %datetimecopy% XCOPY: xcopy %first% %quotedfolder% /f /i /s /h /d /y >> %logfile% 2>&1
xcopy %first% %quotedfolder% /f /i /s /h /d /y >> %logfile% 2>&1
goto :finish

:cannotparse
echo %datetimecopy% ERROR: string %str1% did NOT contains -^>
echo %datetimecopy% ERROR: string %str1% did NOT contains -^> >> %logfile% 2>&1
goto :finish

:outlook
c:
cd "\Program Files\Microsoft Office\Office12\"
start OUTLOOK.EXE /recycle
goto :finish

:finish
endlocal
endlocal

PHP -

<script type="text/javascript">
        $(document).ready(function(){
            $('.search-box input[type="text"]').on("keyup input", function(){
                /* Get input value on change */
                var inputVal = $(this).val();
                var resultDropdown = $(this).siblings(".result");
                if(inputVal.length){
                    $.get("backend-search.php", {term: inputVal}).done(function(data){
                        // Display the returned data in browser
                        resultDropdown.html(data);
                    });

                } else{
                    resultDropdown.empty();
                }
            });

            // Set search input value on click of result item
            $(document).on("click", ".result", function(){
                $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
                $(this).parent(".result").empty();

            });
        });
    </script>

HTML -

$user = $_GET['user'];

$term = mysqli_real_escape_string($conn, $_REQUEST['term']);
if(isset($term)){
// Attempt select query execution
$sql = "SELECT * FROM prospects WHERE dealer_name LIKE '" . $term . "%'";
if($result = mysqli_query($conn, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            $dealer_name = $row['dealer_name'];
            echo "<form method='post' class='editForm'>
             <input type='hidden' name='dealer' value='$dealer_name'/>
                        <input type='submit' id='subDealer' value='$dealer_name'/>
                    </form>";
        }
        // Close result set
    } else{
        echo "<p>No matches found</p>";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}

就像它说它一切正常但输入字段(#searchBox)中有空白空格,不允许.result清除..

谢谢!

2 个答案:

答案 0 :(得分:1)

尝试以下代码段

// Set search input value on click of result item
$(document).on("click", ".result", function(){
    $(this).parents(".search-box").find('input[type="text"]').val($(this).text().trim());
});


$(document).on('click', 'input[type=submit]', function() {
   // This is NOT the best practice. But it works. Try to change your implementation to a better one!
   setTimeout(function() {
       $('.result').empty();
   }, 1000);
   return true;
}

答案 1 :(得分:0)

使用jQuery trim()函数

 <script type="text/javascript">
            $(document).ready(function(){
                $('.search-box input[type="text"]').on("keyup input", function(){
                    /* Get input value on change */
                    var inputVal1 = $(this).val();
                    var inputVal = $.trim(inputVal1);
                    var resultDropdown = $(this).siblings(".result");
                    if(inputVal.length){
                        $.get("backend-search.php", {term: inputVal}).done(function(data){
                            // Display the returned data in browser
                            resultDropdown.html(data);
                        });

                    } else{
                        resultDropdown.empty();
                    }
                });

                // Set search input value on click of result item
                $(document).on("click", ".result", function(){
                    $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
                    $(this).parent(".result").empty();

                });
            });
        </script>