[英]Remove HTML onchange code using jQuery
Below code snippet has HTML onchange attribute as well as jQuery's change event on the input text. 下面的代码片段在输入文本上具有HTML onchange属性以及jQuery的change事件 。 I want only the jQuery's change event to be effective.
我只希望jQuery的change事件有效。 I tried using both unbind and off without luck.
我尝试同时使用unbind和off没有运气。
Could somebody please help? 有人可以帮忙吗?
$(document).ready(function(){ $("#testInput").unbind("change").bind("change",function(){ console.log("jQuery change"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="testInput" type="text" onchange="console.log('change');"/>
If you want to remove the "previous" onchange event, use $("#testInput").removeAttr("onchange")
如果要删除“上一个” onchange事件,请使用
$("#testInput").removeAttr("onchange")
$(document).ready(function() { $("#testInput").removeAttr("onchange") $("#testInput").unbind("change").bind("change", function() { console.log("jQuery change"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="testInput" type="text" onchange="console.log('change');" />
you could set the onchange
to null and add listener, like: 您可以将
onchange
设置为null并添加侦听器,例如:
$(document).ready(function(){
//remove inline 'onchange' event by setting it to null
$("#testInput")[0].onchange = null;
$(document).on("change" "#testInput", function(){
console.log("jQuery change");
});
});
Just remove onchange attribute. 只需删除onchange属性。
removeAttr('onchange')
will remove onchange attribute removeAttr('onchange')
将删除onchange属性
$(document).ready(function(){ $("input").each(function(){ $(this).removeAttr('onchange'); }); $("input").bind("change",function(){ console.log("jQuery change"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="testInput" type="text" onchange="console.log('change');"/> <input id="testInput2" type="text" onchange="console.log('change');"/>
try this 尝试这个
$(document).ready(function(){
$(document).on('change','#testInput',function(){
console.log("jQuery change");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.