简体   繁体   English

防止onclick被解雇

[英]Prevent onclick from firing

I was working around with form submissions in html. 我正在处理html中的表单提交。 Please take a look at below code 请看下面的代码

<form id="form1">
 <button id="btn1" onclick="clicked();">Submit</button>
</form>
<script>
    $("#btn1").click(function (event) {
        alert("event triggered");
        if(some_condition == true){
             // stop firing onclick method but it always submits the form
             event.stopImmediatePropogation(); // not working
             event.preventDefault(); // not working
             event.stopPropogation(); // not working it's for bubbled events
         }
     });
     function clicked(){ alert("clicked me"); }
</script>

I want to stop clicked() function from firing which is attached to inline onclick attribute. 我想停止clicked()附加到内联onclick属性的clicked()函数。 I would like to run my jquery click function and if something goes wrong, I dont want to trigger onclick but it always runs clicked() function. 我想运行我的jquery点击功能,如果出现问题,我不想触发onclick但它总是运行clicked()函数。 Could any one help me. 任何人都可以帮助我。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

The order in which an onxyz handler is called relative to dynamically-attached handlers varies from browser to browser, so your handler may well not run before the original does. 相对于动态连接的处理程序调用onxyz处理程序的顺序因浏览器而异,因此您的处理程序可能无法在原始处理程序之前运行。

To deal with that, you save and remove the onclick handler: 要处理这个问题,请保存并删除 onclick处理程序:

var btn = $("#btn1");
var clickHandler = btn[0].onclick;
btn[0].onclick = false;

Then, in your handler, if you want that function to be called, you call it: 然后,在你的处理程序中,如果你想要调用该函数,你可以调用它:

clickhandler.call(this, event);

Example: 例:

 // Get the button var btn = $("#btn1"); // Save and remove the onclick handler var clickHandler = btn[0].onclick; btn[0].onclick = false; // Hook up your handler $("#btn1").click(function(event) { alert("event triggered"); if (!confirm("Allow it?")) { // Disallowed, don't call it alert("stopped it"); } else { // Allowed, call it clickHandler.call(this, event); } }); // The onclick handler function clicked() { alert("clicked me"); } 
 <form id="form1" onsubmit="return false"> <button id="btn1" onclick="clicked();">Submit</button> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

if condition is true then remove the 'onclick' attribute 如果condition为true,则删除'onclick'属性

if (some_condition == true) {
    $("#btn1").removeAttr('onclick').click(function(event) {
        alert("event triggered");

        //do something
    });
}


function clicked() {
    alert("clicked me");
}

I am sharing a quick workaround without knowing why you cannot add logic to stop adding "onclick="clicked();" code which you are saying getting automatically added. 我正在分享快速的解决方法而不知道为什么你不能添加逻辑来停止添加“onclick =”clicked();“你说的代码会自动添加。

I recommend you hide button with id as "btn1". 我建议你隐藏id为“btn1”的按钮。 Add style display:none. 添加样式显示:无。 You donot need on ready function for this but simply add style attribute to the button btn1 or if that is also not possible directly then use jQuery to do that post document ready. 你不需要为此准备好功能,但只需将样式属性添加到按钮btn1,或者如果这也不可能直接添加,那么使用jQuery来准备该文档。 Read : How to change css display none or block property using Jquery? 阅读: 如何使用Jquery更改css display none或block属性?

Then add a new button to the form using jQuery with id as "btn2" and add register the btn2 click event as well. 然后使用id为“btn2”的jQuery向表单添加一个新按钮,并添加注册btn2 click事件。 DO this after form load. 表格加载后执行此操作。

<form id="form1">
<div id="newbut">
<button id="btn1" onclick="clicked();">Submit</button>
</div>
</form>

jQuery("#newbut").html('<button id="btn2">Submit</button>');
$(document).on('click', '#btn2', function(){ 
     // Your Code
 });

Refer below url to how to register click event for new button: 请参阅以下网址,了解如何为新按钮注册点击事件:

Adding click event for a button created dynamically using jQuery jquery - Click event not working for dynamically created button 为使用jQuery jquery动态创建的按钮 添加click事件 - Click事件不适用于动态创建的按钮

尝试event.stopPropagation() api docs

Can't you do the condition check and the clicked() logic in one function? 你不能在一个函数中进行条件检查和clicked()逻辑吗? ie

<script>
 function clicked() { 
     if(some_condition == true){
         return;
     }
     alert("clicked me");
 }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果其他元素位于顶部,则防止触发onClick - Prevent onClick from firing if another element is on top 如果单击复选框,是否可以防止TD onclick触发? - Prevent TD onclick from firing if a checkbox is clicked? 如何防止嵌套映射元素两次触发 onClick 事件 - How to prevent nested mapped elements from firing onClick event twice 如何使用 React onClick 防止 href 触发? - How can I prevent href from firing using React onClick? 防止 onClick 在没有 preventDefault 的情况下从重叠组件触发? - Prevent onClick firing from overlapping components without preventDefault? Blazor - 防止兄弟事件触发( onclick &gt; onfocusout ) - Blazor - Prevent sibling event from firing ( onclick > onfocusout ) 我有一个带有 onClick 处理程序的 div 以及带有 onClick 事件的按钮。 如何防止按钮触发 div onclick? - I have a div with a onClick handler and also buttons inside with onClick events. How do I prevent the buttons from firing the div onclick? 当 onFocus 触发时,React 会阻止 onClick 触发 - React prevent onClick firing when onFocus fires 反应如何防止在单击孩子时触发父母的 onclick 事件? - React how to prevent a parent's onclick event from firing when a child is clicked? 单击子锚点时,如何防止父项的 onclick 事件触发? - How do I prevent a parent's onclick event from firing when a child anchor is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM