简体   繁体   English

onchange不起作用

[英]onchange does not work

I have this code 我有这个代码

<select id="priority" onchange="myfunction(10);">
    <option>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
</select>

and what i don't understand that it does not trigger the event on the change of the select tag. 而我不明白它不会触发更改选择标记的事件。 Why is that? 这是为什么?

You've told jsfiddle to put your JavaScript code inside a "load" handler. 您告诉jsfiddle将您的JavaScript代码放在“加载”处理程序中。 Thus, your function isn't global and won't be found when the element changes. 因此,您的函数不是全局的,并且在元素更改时将无法找到。

Change the pulldown that currently says "onLoad" to either "no wrap (head)" or "no wrap (body)". 将当前显示为“onLoad”的下拉菜单更改为“no wrap(head)”或“no wrap(body)”。

If you want to stick to your own code, then you have to change the handler on jsFiddle as mentioned by @Pointy. 如果你想坚持自己的代码,那么你必须改变@Pointy所提到的jsFiddle上的处理程序。 But if you want it to be done in another way, then try it as below. 但如果您希望以其他方式完成,请按以下方式尝试。

javaScript : javaScript:

priority.onchange = function myfunction()
{
    alert("On change worked");
}

jQuery : jQuery:

$("#priority").change({id : 10}, function(event){
    alert(event.data.id);
});

Demo 演示

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

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