如何在“选择”标签中编辑“onchange”属性? (使用jquery) [英] How to edit 'onchange' attribute in a 'select' tag? (using jquery)

查看:1031
本文介绍了如何在“选择”标签中编辑“onchange”属性? (使用jquery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为了举例,我有以下代码:

 < select id =sel_idonchange =javascript:foo(); > 

每当我尝试更改其'onchange'属性时,我使用以下命令:

  $(#sel_id)。attr(onchange,foo_2()); 

FYI,这段代码应该不错,onchange属性保持不变。那么你应该如何编辑它?



AMMENDMENT:



属性,然后绑定不同的函数,如:

  $(#sel_id)。removeAttr(onchange change,function(){foo_2();}); 

但问题仍然存在,是否可以更改onchange属性,而不会在第一个

解决方案

不是问题的确切答案,但我可能会删除使用此事件:

  document.getElementById('sel_id')。onchange = undefined; 

(见如何清除/删除JavaScript事件处理程序?



然后继续执行:

  $('#sel_id')。change(function(){foo_2();}); 


I'm trying to edit the 'onchange' event of an existent 'select' element.

For example purposes I have the following code:

<select id="sel_id" onchange="javascript:foo();" >

and whenever I try to change its 'onchange' attribute I was using the following:

$("#sel_id").attr("onchange", "foo_2()");

FYI, this code which should be fine doesn't work, the 'onchange' attribute remains unchanged. So how should you edit it?

AMMENDMENT:

You can remove the attribute and then bind a different function like:

$("#sel_id").removeAttr("onchange").bind("change", function(){ foo_2(); });   

but the issue remains, is it possible to change the 'onchange' attribute without removing it in the first place?

解决方案

Not an exact answer to the question, but I'd probably remove the event using this:

document.getElementById('sel_id').onchange = undefined;

(as seen at How to Clear/Remove JavaScript Event Handler? )

and then go with this:

$('#sel_id').change(function() { foo_2(); });

这篇关于如何在“选择”标签中编辑“onchange”属性? (使用jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆