返回一个对象,显示单击了哪个元素
return an object that show which element was clicked
EDIT我使用闭包和函数这一事实很重要。如gatherData()
和voteSection()
我想单击一个名为submitButton
的div,这应该会告诉我用户做了什么。我有三个部分。投票部分、审查部分和其他类型的投票部分。重点是,现在我正试图建立第一个投票区。当我点击submitButton
时,我应该得到一个看起来像{vote:down}
或{vote:up}
的对象。我在投票部分只有两个按钮。
function gatherData(){
var submitButton = ".submitButton";
var result = {}
function voteSection(){
$(".upButton").click(function(){
// result.vote = "up"
// console.log(result) ;
$(this).data("clicked", true);
$(this).siblings(".downButton").data("clicked",false)
})
$(".downButton").click(function(){
$(this).data("clicked", true);
$(this).siblings(".upButton").data("clicked",false)
// result.vote = "down";
// console.log(result) ;
})
// return result;
}
$(submitButton).on("click",function(){
if($(".upButton").data("clicked")){
result.vote = "up"
}else if($(".downButton").data("clicked")){
result.vote = "down";
}
})
return result;
}
$(".submitButton").on("click",function(){
console.log(gatherData())
})
感谢的帮助
编辑我意识到我忘了打voteSection
这是我现在拥有的。它返回一个空对象
function gatherData(){
var submitButton = ".submitButton";
var result = {}
function voteSection(){
$(".upButton").click(function(){
// result.vote = "up"
// console.log(result) ;
$(this).data("clicked", true);
$(this).siblings(".downButton").data("clicked",false)
})
$(".downButton").click(function(){
$(this).data("clicked", true);
$(this).siblings(".upButton").data("clicked",false)
// result.vote = "down";
// console.log(result) ;
})
if($(".upButton").data("clicked")){
result.vote = "up"
}else if($(".downButton").data("clicked")){
result.vote = "down";
}
// })
return result;
// return result;
}
return voteSection()
// $(submitButton).on("click",function(){
}
$(".submitButton").on("click",function(){
console.log(gatherData())
})
==更新版本2==关闭示例
这里是第二个版本,它使用了一个闭包,该闭包返回一个可以被调用以获取当前状态的函数。
http://codepen.io/anon/pen/xOjaJL
请注意,事件处理程序每次调用voteGatherer()仅绑定到dom 1次,调用voteGatherer(()的结果是一个函数,您可以在需要投票状态时调用该函数。
function voteGatherer()
{
var state = { 'vote': null }; // no vote selection made
$(".my-voting-button").click(function(ev)
{
var target = $(ev.target);
state[target.data("action")] = target.data("value");
});
return function()
{
return state;
}
}
var gatherer1GetState = voteGatherer();
$(".my-submit-button").click(function(ev)
{
$("#stateString").html(JSON.stringify(gatherer1GetState())) ;
});
===版本1
在使用sumbit之前,我准备了一支代码笔来帮助您管理这些部分的状态。看看吧。
http://codepen.io/anon/pen/bZrxRk
<button data-value="up" data-action="vote" class="btn btn-primary my-voting-button">Vote Up</button>
<button data-value="down" data-action="vote" class="btn btn-warning my-voting-button">Vote Down</button>
请注意,投票按钮具有在状态对象上更新的操作和值。
因此,当您单击向上投票按钮时,state["vote"]
值将设置为"up"
。这可以应用于您的所有字段。您可以将"我的投票按钮"类添加到其他按钮中(也可以将该类重命名为更适合您的用例的类)。
以下是更新状态对象的javascript:
var state = { 'vote': null }; // no vote selection made
$(".my-voting-button").click(function(ev)
{
var target = $(ev.target);
state[target.data("action")] = target.data("value");
});
现在您已经有了一个完全填充的状态对象,您只需使用ajax将该对象汇总回服务器即可。在我的例子中,我把它串起来写在纸上。
$(".my-submit-button").click(function(ev)
{
$("#stateString").html(JSON.stringify(state)) ;
});
下面是三个按钮和一个div
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://code.jquery.com/jquery-3.1.0.slim.min.js" integrity="sha256-cRpWjoSOw5KcyIOaZNo4i6fZ9tKPhYYb6i5T9RSVJG8=" crossorigin="anonymous"></script>
<!--<script src="index.js"></script>-->
<title>repl.it</title>
</head>
<body>
<button class="upButton">up</button>
<button class="downButton">down</button>
<button class="submitButton">submit</button>
<div id="vote">vote goes here </div>
</body>
</html>
您可以使用类似的方法来获取给定前一个html文档的投票值。
$( document ).ready(function() {
var result = {vote:""}
$(".upButton").click(function(){
result.vote = "up";
});
$(".downButton").click(function(){
result.vote = "down";
});
$(".submitButton").click(function(){
$("#vote").html(result.vote);
});
});
- 对同一类中的所有元素单击一次
- 元素单击处理程序由一个神秘的函数取消设置
- 在处理元素单击事件期间,在循环内部调用window.open()
- JS-是否可以获得元素单击位置的确切宽度
- 使用 jQuery 创建动态元素 单击事件始终返回 for 循环中的最后一个索引
- 选中父元素单击上的复选框
- 在窗口和其他元素单击时隐藏
- Polymerjs 的自定义菜单元素单击事件
- 使用 javascript 函数查找元素单击的类
- 元素单击仅使用 javascript
- 等待文档鼠标按下完成,然后元素单击才能开始
- 由于父元素的顶部和位置,子元素单击不起作用
- Jquery-在共享相同类名的其他元素之间选择一个元素(单击时)
- 目标元素单击子元素,获取父属性
- 根据元素单击更改选择选项值
- 为listview上动态生成的元素单击event
- 扩展原生元素单击事件
- 可以被许多元素使用的Javascript函数,然后区分哪个元素单击了它
- 当元素单击时隐藏iframe
- 按钮元素单击事件以切换类不起作用