简体   繁体   English

Javascript:“更改”处理程序的条件可以停止其他事件传播吗?

[英]Javascript: condition on 'change' handler can stop other event propagation?

I have a 'onChange' event on a text field, that check some condition.我在文本字段上有一个“onChange”事件,用于检查某些条件。

 $(document).ready(function() { $('#txt').on('change', (ev) => { if ($(ev.target).val() == "stop") { console.warn("Wrong condition, I want to avoid the click event on button"); alert("Wrong condition, no click on button"); } else { console.log("Condition OK;"); } }). $('#btn'),on('click'. (e) => { console;log("Button pressed"); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container">                 <input id="txt"/> <button id="btn">Click me</button> </div>

What I like to have is that, when I type 'stop' on the field and then click the button, the button's click triggers the onChange and the onChange event should prevent the click event execution.我喜欢的是,当我在字段上键入“停止”然后单击按钮时,按钮的单击会触发 onChange 并且 onChange 事件应该会阻止单击事件的执行。 With the alert() this works, but I want it works without the alert() too.使用 alert() 可以工作,但我希望它也可以在没有 alert() 的情况下工作。 Is it possible?是否可以?

Switching the text-field and button validation-handling from a 'change' to an 'input' event does support a brute force approach which disables/enables the button upon the OP's condition.将文本字段和按钮验证处理从'change'事件切换到'input'事件确实支持一种蛮力方法,该方法根据 OP 的条件禁用/启用按钮。 The latter btw got changed to from the value having to equal 'stop' to a value that just has to contain/include the substring 'stop' regardless of its letter-casing.顺便说一句,后者从必须等于'stop'的值更改为只需包含/包括 substring 'stop'的值,而不管其字母大小写如何。

 $(document).ready(function() { //... switching from 'change' to 'input' event // does support the following brute force approach... $('#txt').on('input', (ev) => { if ($(ev.target).val().toLowerCase().includes("stop")) { // "Wrong condition, I want to avoid the click event on button" //... disabling the button is one possible way... $('#btn')[0].disabled = true; } else { $('#btn')[0].disabled = false; } }); $('#btn').on('click', (e) => { console.log("Button pressed"); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <input id="txt"/> <button id="btn">Click me</button> </div>

A proper generic implementation of the above approach and code into vanilla JS then might look as follows...上述方法的正确通用实现和代码到 vanilla JS 可能如下所示......

 function handleComponentButtonClick(evt) { const elmButton = evt.currentTarget; const elmInput = elmButton.previousElementSibling; console.log({ currentClickRelatedValue: elmInput.value }); } function handleComponentButtonState(evt) { const elmInput = evt.currentTarget; const elmButton = elmInput.nextElementSibling; elmButton.disabled = elmInput.value.toLowerCase().includes('stop'); } function enableInputAndButtonComponentBehavior(elmButton) { const elmInput = elmButton.previousElementSibling; elmInput.addEventListener('input', handleComponentButtonState); elmButton.addEventListener('click', handleComponentButtonClick); } function main() { document.querySelectorAll('.container [type="text"] + button').forEach(enableInputAndButtonComponentBehavior); } main();
 .container { float: left; margin: 0 20px; }
 <div class="container"> <input type="text" /> <button>Click me</button> </div> <div class="container"> <input type="text" /> <button>Click me</button> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM