【发布时间】:2019-12-22 07:55:11
【问题描述】:
所以我现在遇到了问题。在我的代码中,我在向用户显示我的代码时会遍历一个数组。但是,当我想使用 JQuery 选择一个元素以使用 .val 方法进行更改时,它会更改具有该类名的所有元素吗?我是 Node 和 JQuery 的新手,所以请指出任何重复项。我怎样才能使它在点击元素时只有该元素发生变化?
JQuery 代码:
$('.likebutton').on('click', function(){ // hit like
console.log("like clicked?"); // works
// i need to find a way to connect this with code and make it update???
var curr = $('#changeit').val();
var item = $('.likebutton').val('Liked'); // problem
console.log("item: " + curr);
$.ajax({ //do something with the data via front-end framework, so we can update in reall time
type: 'GET',
url: '/',
success: function(err){
// here we will get the data, find it in the data base and update it. Changes will occur
// via a reload, either manually or when they create a post, for now. Later we can update
// the thing in real time.
// on click we must also change the color of the thing too to show that it was liked
console.log('success!');
}
});
return false;
});
EJS 代码
<!DOCTYPE html>
<html>
<head>
</head>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="/assets/chat.js"></script>
<body>
<h1> Ut whispers</h1>
<form action="/" method="POST">
<input type="text" placeholder = "say something" name="msg"> <input type="submit" value="Submit"> <br>
</form>
<ul>
<% for(var i = 0; i < chats.length; i++) { %>
<li class = "display"><%= chats[i].msg %> <input type="button" id = "changeit" class = "likebutton" value = "Like: (<%=chats[i].likes%>)" >
<ul>
<li>replies will go here</li>
</ul>
</li>
<% } %>
</ul>
</body>
</html>
【问题讨论】:
标签: javascript jquery node.js ejs