简体   繁体   English

获取文本框值而不是自动完成值javascript

[英]get textbox value instead of auto-completion value javascript

I have a textbox with jQuery auto-completion feature. 我有一个带有jQuery自动补全功能的文本框。 What I need is: when I click on the button "Movies by this title", I would like to see the list of movies contain the term in the textbox (even if it is not found in auto-completion), while I see the list of movies by only auto-completion select. 我需要的是:当我单击“按此标题显示的电影”按钮时,我希望在文本框中看到包含该术语的电影列表(即使未在自动完成中找到),仅通过自动完成选择的电影列表。

The image below shows the case when I select a movie from auto-completion list (which works well): 下图显示了当我从自动完成列表中选择电影时的情况(效果很好): 在此处输入图片说明

and this is the case when I write just some words (eg if user forgot the whole name or couldn't find in auto-complete list): 这就是我只写一些单词的情况(例如,如果用户忘记了全名或在自动完成列表中找不到):

在此处输入图片说明

here is the code: 这是代码:

<script type="text/javascript">
var selected;

$(document).ready(function () {
      $("input[id='selectType']").change(function(){
               if ($(this).val() == "byTitle") {
                    $("#m_scents2").hide();
                    $("#btnMove").show();
                    $("#m_scents").show();
                    $("#q").focus();
                    $("#q").autocomplete({
                        minLength: 0,
                        delay:5,
                        source: "filmsauto.php",
                        focus: function( event, ui ){
                             event.preventDefault(); 
                             return false;
                        },
                select: function( event, ui ) {
                               window.selected = ui.item.value;
                        }
                    });
  }
$('#btnMove').on('click', function (e) {
           popupCenter("movieBytitle.php","_blank","400","400");
  });

This is movieBytitle.php: 这是movieBytitle.php:

<script type="text/javascript">
 var selected = parent.window.opener.selected; 
 $.ajax({
         url: 'childfilm.php',
         datatype: "json",
         data:{p:selected},
         success: function(response) {      
                     $("#field").html(response);
                   }
        });

</script>

and this is childfilm.php: 这是childfilm.php:

if(isset($_GET['p']) && !empty($_GET['p'])){
    include('imdbConnection.php');
    $query = $conn->prepare("SELECT DISTINCT movieName, year, posterLink FROM film_info WHERE movieName LIKE :p");
    $query->execute(array(':p' => '%'.$_GET['p'].'%'));
}
?>

<html>
<head>
....
</head>

<body>
 <div class= "movielist"> 
  <table id= "films">
    <tr>
        <th></th>
        <th>year</th>
        <th>Title</th>
    </tr>
    <?php while($row = $query->fetch(PDO::FETCH_ASSOC)): ?>  //THIS IS LINE 53 WHERE THE ERROR IS SHOWN IN THE SECOND IMAGE
    <tr>
        <td><img class='imdbImage' id="image" src='imdbImage.php?url=<?php echo $row['posterLink']; ?>' alt="" /></td>
        <td><label id='year'><?php echo $row['year']; ?> </label></td>

        <td><a href="movie.php?title=<?php echo urlencode($row['movieName']); ?>"><?php echo $row['movieName']; ?></a></td>

    </tr>
    <?php endwhile; ?>
  </table>    
 </div>
</body>
</html>

Could someone kindly let me know how I can get the textbox value instead of auto-complete value ?? 有人可以让我知道如何获取文本框值而不是自动完成值吗?

UPDATE: FINAL SOLUTION 更新:最终解决方案

Since the question is too long and the final solution was achieved in comments, I updated the answer here: Thanks to @lolka_bolka, this finally helped me: change the value of p to --> ($('#q').val()) instead of passing selected auto-complete text.. :) 由于问题太长且最终解决方案已通过注释实现,因此我在此处更新了答案:感谢@lolka_bolka,这最终帮助了我:将p的值更改为-> ($('#q').val())而不是传递所选的自动完成文本。.:)

EDIT: 编辑:

Based on OP update, we made the conversation in the comments, please read them. 基于OP更新,我们在评论中进行了对话,请仔细阅读。

I think, your problem will be that, if there are not $_GET["p"] , then the $query won't exists, so you can not use it as an object. 我认为,您的问题将是,如果没有$_GET["p"] ,则$query将不存在,因此您不能将其用作对象。

<html>
    <head>
        ....
    </head>
    <body>
        <div class= "movielist"> 
            <table id= "films">
                <tr>
                    <th></th>
                    <th>year</th>
                    <th>Title</th>
                </tr>
                <?php
                if (isset($_GET['p']) && !empty($_GET['p'])) {
                    include('imdbConnection.php');
                    $query = $conn->prepare("SELECT DISTINCT movieName, year, posterLink FROM film_info WHERE movieName LIKE :p");
                    $query->execute(array(':p' => '%' . $_GET['p'] . '%'));
                    if ($query->rowCount()) {
                        //If there are no $_GET["p"] then there wont be $query,
                        //So you can not fetch it!!!
                        while ($row = $query->fetch(PDO::FETCH_ASSOC)):
                            ?> 
                            <tr>
                                <td><img class='imdbImage' id="image" src='imdbImage.php?url=<?php echo $row['posterLink']; ?>' alt="" /></td>
                                <td><label id='year'><?php echo $row['year']; ?> </label></td>

                                <td><a href="movie.php?title=<?php echo urlencode($row['movieName']); ?>"><?php echo $row['movieName']; ?></a></td>

                            </tr>
                            <?php
                        endwhile;
                    } else {
                        echo '<td colspan="3">Sorry, there are no film like this</td>';
                    }
                }
                ?>
            </table>    
        </div>
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM