如何在一个onclick函数中添加两个响应?html中的onclick是
How to add two responses to one onclick function? The onclick in html is
我正试图弄清楚,如果使用其他点击,如何获得一个点击函数来做一些新的事情。我有三个onclick
函数,每个函数在单击时都有单独的反馈语句。他们连接的div
看起来像这样:
$('#p26_opt1, #p26_opt2, #p26_opt3').click(function() {
// check whether this div was selected already
var wasSelected = $(this).data('selected');
document.getElementById("p26_feedback1").style.display = 'block';
document.getElementById("p26_feedback2").style.display = 'none';
// change its selected state to the opposite of what it was before
$(this).data('selected', !wasSelected);
// keep track of whether any checkboxes are un-checked with a flag
var anyUnselected;
// iterate through all the divs
$('#p26_opt1, #p26_opt2, #p26_opt3').each(function() {
// if there was already an un-selected div, or if this div is
// un-selected, set our flag
anyUnselected = anyUnselected || !$(this).data('selected');
});
// if our flag indicates any were un-seelcted, stop here
if (anySelected) return;
// do whatever you want to do when all three are checked.
$('#p26_feedback3').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="p26_options">
<div id="p26_opt1">Periodically review the data stored on your devices to determine what you no longer need</div>
<br>
<div id="p26_opt2">Delete the data and empty the recycle bin</div>
<br>
<div id="p26_opt3">Follow the Defense Department guidelines to get rid of all traces of the data on your computer</div>
<br>
<div id="p26_opt4">Put it on your to do list for next week</div>
<br>
</div>
<div id="p26_feedback1">And what else should you do? There are more correct answers.</div>
<div id="p26_feedback2">Not quite – why not do it now instead? Please try again.</div>
<div id="p26_feedback3">Yes! To completely dispose of electronic data, you need to review and identify old data, delete it and empty your recycle bin and take further measures to get rid of all traces of it on your device.</div>
我可以选择每个选项并得到单独的回复。我的问题是,只有同时选择了这三个选项,我才能显示第二个反馈声明?
在Javascript中,每当你想做任何事情来响应发生的事情时,你必须将你的逻辑与对应于"正在发生的事情"的相关事件的处理程序联系起来。在这种情况下,场景的相关部分是,当用户单击一个时,您希望检查选择了哪些元素。在确定"点击"是您想要针对的事件之后,您的下一步是添加一个onClick
处理程序。
具体来说,您想要做的是向所有三个元素添加一个onClick
处理程序,该处理程序检查是否检查了其他两个元素(如果是,则创建所需的反馈)。例如,假设你有三个复选框,都有一个类"复选框":
$('.checkbox').click(function() {
// keep track of whether any checkboxes are un-checked with a flag
var anyUnchecked;
// iterate through all the checkboxes
$('.checkbox').each(function() {
// if there was already an un-checked checkbox, or if this checkbox is
// un-checked, set our flag
anyUnchecked = anyUnchecked || !$(this).is(':checked');
});
// if our flag indicates any were un-checked, stop here
if (anyUnchecked) return;
// do whatever you want to do when all three are checked.
$('#secondaryFeedbackStatement').show();
});
-编辑-
由于您不处理复选框(它们跟踪自己的状态),因此需要跟踪是否以某种方式选择了每个<div>
。幸运的是,jQuery提供了一种在元素上保存任意数据的机制,称为data
。以下是如何使用它:
$('#p26_opt1, #p26_opt2, #p26_opt3').click(function() {
// check whether this div was selected already
var wasSelected = $(this).data('selected');
// change its selected state to the opposite of what it was before
$(this).data('selected', !wasSelected);
// keep track of whether any checkboxes are un-checked with a flag
var anyUnselected;
// iterate through all the divs
$('#p26_opt1, #p26_opt2, #p26_opt3').each(function() {
// if there was already an un-selected div, or if this div is
// un-selected, set our flag
anyUnselected = anyUnselected || !$(this).data('selected');
});
// if our flag indicates any were un-seelcted, stop here
if (anySelected) return;
// do whatever you want to do when all three are checked.
$('#secondaryFeedbackStatement').show();
});
var a = false
var b = false
var c = false
onclickA(){
//general onclickA code
a = true
if (a && b && c){
//execute your desired code
}
}
onclickB(){
//general onclickBcode
b = true
if (a && b && c){
//execute your desired code
}
}
onclickC(){
//general onclickC code
c = true
if (a && b && c){
//execute your desired code
}
}
如果这就是你想要的。
相关文章:
- 如果从onclick()内部调用app.open,则它没有响应
- 响应菜单问题-如何在移动环境中处理HREF和onclick事件
- 等待anchor标记onclick函数响应为true
- ajax 响应包括 image,onClick 事件对图像不起作用 jQuery
- 如何在 javascript 中延迟元素的 onclick 响应
- onClick不响应hide()和show()函数
- 如何在一个onclick函数中添加两个响应?html中的onclick是
- JS函数不响应事件onclick
- 客户端在收到整个响应之前关闭了连接-onClick跟踪错误
- 在响应上添加onclick链接
- 动态地为引导分页生成li元素,以便它们响应onclick事件
- aspx页面中的锚标记onclick事件在IE(11)中工作,但在Chrome和Firefox中无法响应
- Jquery onclick不工作或响应
- 我怎么能强迫元素在一个固定的定位元素响应javascript的onclick事件在Android web浏览器
- 使用onclick事件-jquery的响应绑定文本框
- JS onclick事件处理程序-没有响应
- 如何使用CSS3更改响应onClick事件的不透明度过渡图像
- 内联onclick事件对ajax响应不起作用
- 当ajax响应到达时,Onclick准备HTML并在react中呈现
- 如何停止多个事件处理程序onclick的html从服务响应