我想弄清楚如果使用其他点击,如何让点击功能执行新功能。我有三个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>
我可以选择每个选项并获得单独的回复。我的问题是,如果同时选择所有三个,我该如何才能显示第二个反馈声明?
答案 0 :(得分:0)
在Javascript中,无论何时你想做任何事情来回应发生的事情,你都必须将你的逻辑绑定到一个相应事件的处理程序,该事件对应于发生的事情&#34;。在这种情况下,您的方案的相关部分是您要在用户单击一个时检查选择了哪些元素。确定了&#34;点击&#34;是您要定位的事件,下一步是添加onClick
处理程序。
具体来说,您要做的是向所有三个元素添加onClick
处理程序,以检查是否检查了其他两个元素(如果是,则创建所需的反馈)。例如,假设你有三个复选框都带有一个&#34;复选框&#34;:
$('.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();
});
答案 1 :(得分:0)
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
}
}
如果那就是你要找的东西。