gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:48:27 25 4
gpt4 key购买 nike

我有一个具有 jQuery 自动完成功能的文本框。我需要的是:当我单击“此标题的电影”按钮时,我希望看到文本框中包含该术语的电影列表(即使在自动完成中找不到),而我看到电影列表只能通过自动完成选择。

下图显示了我从自动完成列表中选择电影时的情况(效果很好): enter image description here

当我只写一些单词时就是这种情况(例如,如果用户忘记了整个名称或在自动完成列表中找不到):

enter image description here

这是代码:

<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");
});

这是 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>

这是 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>

有人可以告诉我如何获取文本框值而不是自动完成值吗?

更新:最终解决方案

由于问题太长,最终的解决方案是在评论中实现的,我在这里更新了答案:感谢@lolka_bolka,这终于帮助了我:将 p 的值更改为 --> ($(' #q').val()) 而不是传递选定的自动完成文本..:)

最佳答案

编辑:

根据OP更新,我们在评论中进行了对话,请阅读。

我认为,你的问题是,如果没有 $_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>

关于javascript - 获取文本框值而不是自动完成值javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com