点击时Jquery会多次显示警报消息

时间:2016-01-14 04:04:48

标签: javascript jquery html

我的警报信息被显示三次甚至4次都无法识别问题,但我知道我需要解除绑定但不要放在哪里。任何帮助都会受到赞赏。谢谢你



jQuery('#compleave,#prevleave,#sickleave,#vacation').on('click', function(event) {     
    
	
	jQuery('#show2').show();
     $("#but").remove();
     if (this.id == 'prevleave') {

    	 document.getElementById("ldays").value = but1_val2;
         document.getElementById("ltype").value = "Privilege Leave";
     }
     else if (this.id == 'compleave') {
    	 document.getElementById("ldays").value = but1_val1;
         document.getElementById("ltype").value = "Dubai Casual Leave"; 
     }
     else if (this.id == 'sickleave') {
    	 document.getElementById("ldays").value = but_val3;
         document.getElementById("ltype").value = "Sickness";
     }
     else if (this.id == 'vacation') {
    	 document.getElementById("ldays").value = but_val4;
         document.getElementById("ltype").value = "Vacation";
     }
    

     $("#apply").click(funcapply);
});
function funcapply()
{
  alert("HELLO CHECK THIS");
}

<div id="but">
<input type="button" id="prevleave" class="appButton1" value="Privilege Leave"> <br> <br>
<input type="button" id="compleave" class="appButton1" value="Dubai Casual Leave"> <br> <br>
<input type="button" id="sickleave" class="appButton1" value="Sickness"> <br> <br>
<input type="button" id="vacation" class="appButton1" value="Vacation"> <br> <br>
</div>

<div id="show2" class="container1" style="display:none;">
 
<label>LEAVE DAYS: </label> <input type="text" id="ldays"><br><br>
<input type="submit" class="applleave" value="APPLY LEAVE" id="apply" onclick="funcapply();">
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您正在调用funcapply函数两次。 只需将输入html替换为

即可
<input type="submit" class="applleave" value="APPLY LEAVE" id="apply"/>

表示您需要从中删除onclick事件,因为它已经被使用

$("#apply").click(funcapply);

答案 1 :(得分:0)

$("#apply").click(funcapply);从onClick事件移至

$(document).ready(function() {
   $("#apply").click(funcapply);
})

只有在标记准备好接收事件后,才会收到点击事件。即你的案例中点击事件。

<强> JSFiddle

答案 2 :(得分:0)

试试这个,

   $("#apply").click(funcapply); // when click on apply leaves

   jQuery('#compleave,#prevleave,#sickleave,#vacation').click(function(event) {
     jQuery('#show2').show();
     $("#but").remove();
     if (this.id == 'prevleave') {

       document.getElementById("ldays").value = but1_val2;
       document.getElementById("ltype").value = "Privilege Leave";
     } else if (this.id == 'compleave') {
       document.getElementById("ldays").value = but1_val1;
       document.getElementById("ltype").value = "Dubai Casual Leave";
     } else if (this.id == 'sickleave') {
       document.getElementById("ldays").value = but_val3;
       document.getElementById("ltype").value = "Sickness";
     } else if (this.id == 'vacation') {
       document.getElementById("ldays").value = but_val4;
       document.getElementById("ltype").value = "Vacation";
     }


   });

   function funcapply() {
     alert("HELLO CHECK THIS");
   }

Run Demo