所以这份名单有效了!现在轮到我的下一招了。我希望用户能够对那些列出的电影投票多少次,当他们投票的时候,我希望那些被投票的电影被列在“你是最好的电影”下面。到目前为止,我得到的是:
在HTML中:
<!DOCTYPE html>
<html>
<link rel="stylesheet" text="text/css" href="movies.css">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<title>
Hello World
</title>
</head>
<body>
<h1>My Favorite Movies</h1>
<div id = "first">
<input type="text" id="movie" placeholder="Movie">
<button id="enter">Enter</button>
</div>
<div id="list"><u>Chosen Films:</u></div>
<!-- <div>Chosen Films: <span id="list"></span></div> -->
<!-- <div id="films"></div> -->
<div id="best">You're Best Films:</div>
<script type="text/javascript" src="movies.js"></script>
</body>
</html>在我的js中:
/ The function that creates the first list
$("#enter").click(
function() {
var movie = $("#movie").val();
var list = (movie);
$('#list').append('<p>' + '<button id="vote"></button>' + list + '</p>');
});
// The function that is supposed to place the chosen movies in a list below "Your best films"
$("#vote").click(
function() {
var voted = $("#best").val();
var best = (voted);
$('#best').append('<p>' + list + '</p>');
});所以第一个函数可以工作,只需要第二个函数。
发布于 2015-11-06 17:55:20
首先,不能从第二个函数中计算list。它超出了范围。所以你需要把它储存起来或者传递出去。我建议将它存储在按钮的值中。
工频:http://jsfiddle.net/Twisty/810353L9/
JQUERY
// The function that creates the first list
$("#enter").click(function () {
var movie = $("#movie").val();
var $btn = $('<button />', {
id: "vote",
type: "button",
text: "^",
value: movie,
click: function(){
var vote = this.value;
$("#best").append('<p>'+vote+'</p>');
}
});
var $p = $('<p />');
//$('#list').append('<p>' + '<button id="vote" value="'+list+'">Vote</button> '+list+'</p>');
$p.append($btn, " ", movie);
$p.appendTo("#list");
});
// The function that is supposed to place the chosen movies in a list below "Your best films"
/*$("#vote").live('click', function () {
var voted = $(this).attr("value");
console.log(voted);
$('#best').append('<p>' + voted + '</p>');
});*/这将创建“投票”按钮,“投票”按钮将追加下一个列表。
https://stackoverflow.com/questions/33571251
复制相似问题