防止在页面加载时触发Onchange事件

时间:2018-02-12 11:54:35

标签: javascript jquery html html5

我正在使用Onchange事件,它工作正常。但我希望它能防止页面加载时触发。

$("#first").change(function(){
   $("#salary").val(function () {
    return $(this).find('option').filter(function () {
      return $(this).prop('null');
        }).val();
 });
});

任何人都可以通过此代码帮助实现这一目标吗?

先谢谢。

2 个答案:

答案 0 :(得分:1)

<html>
 <head>
  <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<script>
$( window ).on( "load", function() {
   $("#first").change(function(){
    $("#salary").val(function () {
      return $(this).find('option').filter(function () {
        return $(this).prop('null');
        }).val();
    });
  });
});
</script>
</html>

答案 1 :(得分:0)

您可以阅读此网址https://learn.jquery.com/using-jquery-core/document-ready/,然后解决。

<html>
 <head>
  <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script>
  $( document ).ready(function() {
      console.log( "document loaded" );
  });

  $( window ).on( "load", function() {
      console.log( "window loaded" );
  });
  </script>
 </head>
</html>
相关问题