Javascript 移除项目后,无法将其重新添加到篮(数组)中
我的PHP表单中有一个篮子(一个数组),用户可以在其中添加他们选择的电影。 下图可以更好地显示情况: 问题: 如图所示,用户可以单击垃圾图像从列表中删除电影。现在的问题是,如果他/她想再次将此已删除的电影添加到所选列表中,则会显示“此电影已在列表中”消息,而仅当该电影现在不在列表中时才应显示该消息 这是我的代码(form.php):Javascript 移除项目后,无法将其重新添加到篮(数组)中,javascript,php,arrays,Javascript,Php,Arrays,我的PHP表单中有一个篮子(一个数组),用户可以在其中添加他们选择的电影。 下图可以更好地显示情况: 问题: 如图所示,用户可以单击垃圾图像从列表中删除电影。现在的问题是,如果他/她想再次将此已删除的电影添加到所选列表中,则会显示“此电影已在列表中”消息,而仅当该电影现在不在列表中时才应显示该消息 这是我的代码(form.php): var master_basket=新数组(); var selectedMovies={}; $(文档).ready(函数(){ ///一些与我的问题无关的代
var master_basket=新数组();
var selectedMovies={};
$(文档).ready(函数(){
///一些与我的问题无关的代码。。。。
函数获取\u项的\u索引(电影\u名称){
对于(变量i=0;i
这是我更新的searchfilm.php:
<?php
// QUERY DB
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="http://www.imdb.com/title/<?php echo urlencode($row['ImdbId']); ?>"><?php echo $row['movieName']; ?></a></td>
<td><a href="javascript:addmovie('<?php echo $row['movieName'], "_", $row['year']; ?>')" class="add">Add to list</a></td>
</tr>
<?php
endwhile;
?>
<script type="text/javascript">
function addmovie(movieName) {
var obj = {
"movie_name":movieName
};
var index = parent.window.opener.get_index_of_item(movieName);
if(index === -1) {
parent.window.opener.addToBasket(obj);
} else {
alert("This movie is already in the selected list");
}
}
</script>
“alt=”“/>
函数addmovie(movieName){
var obj={
“电影名称”:电影名称
};
var index=parent.window.opener.get\u项的索引(movieName);
如果(索引==-1){
parent.window.opener.addToBasket(obj);
}否则{
警报(“此电影已在所选列表中”);
}
}
有人能帮我修一下吗?你的.indexOf()
搜索正在寻找一个字符串“矩阵1999”
,但是篮子里没有字符串。它装着所有电影信息的对象。所以你的.indexOf()
返回了-1
您需要一个函数,该函数将根据您指定的名称和年份在篮子中搜索正确的对象
此外,不需要将名称/年份作为属性添加到篮子中
var master_basket = new Array();
/*** This will receive the movie name/year,
and return the index or -1 if not found ***/
function get_index_of_item(movie_name, movie_year) {
for (var i = 0; i < master_basket.length; i++) {
if (master_basket[i].movie_name == movie_name &&
master_basket[i].movie_year == movie_year {
return i;
}
}
return -1;
}
function addToBasket(item) {
master_basket.push(item);
showBasketObjects();
}
// We need to pass along the movie name and year so it can be used to
// fetch the right index from the array.
function showBasketObjects() {
$("#basket_content").empty();
$.each(master_basket, function(k, v) {
var name_and_year = v.movie_name + "_" + v.movie_year;
$("#basket_content").append("<div class='item_list'>" +
v.movie_name +
"<a class='remove_link' href='" +
name_and_year +
"'><img width='20' src='http://i61.tinypic.com/4n9tt.png'></a></div>");
});
$(".remove_link").on('click', function (e) {
// Split the `href` on the `_` character to get the name and year
var name_year = $(this).attr("href").split("_");
// The split gave us an Array. The name is at the first index,
// and the year is at the second
var index = get_index_of_item(name_year[0], name_year[1]);
// We check to see if it returned `-1`, and if not, we remove it
if (index !== -1) {
master_basket.splice(index, 1);
console.log(master_basket);
$(this).closest('.item_list').remove();
} else {
console.log("DEBUG: For some reason it was not in the list");
}
return false;
});
}
你没有调试的方法吗?你在使用哪个IDE?在最坏的情况下,如果没有IDE(yeuch!),你可以在代码中加入一些alert(),看看什么是错误的。我删除了我的答案,因为我很困惑。你有
addToBasket()
和addmovie()
。一个是将项目推送到master_-basket
数组中,另一个是将它们作为命名属性添加到master_-basket
数组对象中。为什么两者都有?在任何情况下,都是将对象添加到数组中,然后尝试获取其.indexOf()
使用其名称和年份。这是行不通的。它无法知道字符串是否应与电影名称/movie\u year
属性进行比较。您需要手动搜索数组。@sixfingeredman:我在表单的主页面中有addToBasket函数,其中有一个文本框和搜索按钮。addmovie
是我在另一个页面中使用的函数(其中有DB查询),其中包含更多的代码,如果你看到的话..你认为我错了吗?@sixfingeredman:我想问题是因为我删除了.item\u list'
div,而movieName仍然在主框中..所以,我想我必须将它从篮框列表中删除,而不是删除div..你认为呢?
var master_basket = new Array();
/*** This will receive the movie name/year,
and return the index or -1 if not found ***/
function get_index_of_item(movie_name, movie_year) {
for (var i = 0; i < master_basket.length; i++) {
if (master_basket[i].movie_name == movie_name &&
master_basket[i].movie_year == movie_year {
return i;
}
}
return -1;
}
function addToBasket(item) {
master_basket.push(item);
showBasketObjects();
}
// We need to pass along the movie name and year so it can be used to
// fetch the right index from the array.
function showBasketObjects() {
$("#basket_content").empty();
$.each(master_basket, function(k, v) {
var name_and_year = v.movie_name + "_" + v.movie_year;
$("#basket_content").append("<div class='item_list'>" +
v.movie_name +
"<a class='remove_link' href='" +
name_and_year +
"'><img width='20' src='http://i61.tinypic.com/4n9tt.png'></a></div>");
});
$(".remove_link").on('click', function (e) {
// Split the `href` on the `_` character to get the name and year
var name_year = $(this).attr("href").split("_");
// The split gave us an Array. The name is at the first index,
// and the year is at the second
var index = get_index_of_item(name_year[0], name_year[1]);
// We check to see if it returned `-1`, and if not, we remove it
if (index !== -1) {
master_basket.splice(index, 1);
console.log(master_basket);
$(this).closest('.item_list').remove();
} else {
console.log("DEBUG: For some reason it was not in the list");
}
return false;
});
}
// I think the `movieName` is getting `"The Matrix_1999"`, so split it on `"_"`
function addmovie(movieName) {
var name_and_year = movieName.split("_");
var obj = {
"movie_name":name_and_year[0],
"movie_year":name_and_year[1]
};
// Use the function to see if the item is already in the list
var index = parent.window.opener.get_index_of_item(movieName, year);
// If it returned `-1`, we can add it.
if(index === -1) {
parent.window.opener.addToBasket(obj);
} else {
alert("This movie is already in the selected list");
}
}