【问题标题】:Jquery execute onchange event on onloadJquery在onload时执行onchange事件
【发布时间】:2013-02-20 16:12:46
【问题描述】:

我有一个函数可以在更改事件时运行发布操作。

$("select#marca").change(function(){
    var marca = $("select#marca option:selected").attr('value');
    $("select#modello").html(attendere);
$.post("select.php", {id_marca:marca}, function(data){
        $("select#modello").html(data);
    });
});

我想在加载事件时执行这个功能。可能吗? 有什么好办法吗?

【问题讨论】:

  • 您的意思是在加载时触发该功能?你试过了吗:$("select#marca").change();

标签: javascript jquery


【解决方案1】:

只需将它放在一个函数中,然后在文档准备好时调用它,就像这样:

$(function () {
    yourFunction(); //this calls it on load
    $("select#marca").change(yourFunction);
});

function yourFunction() {
    var marca = $("select#marca option:selected").attr('value');
    $("select#modello").html(attendere);
    $.post("select.php", {id_marca:marca}, function(data){
        $("select#modello").html(data);
    });
}

或者只是在页面加载时调用change

$(function () {
    $("select#marca").change();
});

【讨论】:

  • 我试过 $(window).load(function () { $(function () { $("select#marca").change(); }); 但事件没有运行..
  • @PaoloRossi $(function()在页面加载时执行 不要将其包装在 $(window).load 中,并确保在尝试之前已注册 change 事件开火.change
  • 完美!非常感谢!
【解决方案2】:

真正简单的方法是将另一个 .change() 事件链接到你的 on change 函数的末尾,如下所示:

$("#yourElement").change(function(){
   // your code here
}).change(); // automatically execute the on change function you just wrote

【讨论】:

    【解决方案3】:

    如果在末尾加上.change(),绑定后会直接调用:

    $(function() { 
        $("select#marca").change(function(){
            var marca = $("select#marca option:selected").attr('value');
            $("select#modello").html(attendere);
        $.post("select.php", {id_marca:marca}, function(data){
                $("select#modello").html(data);
            });
        }).change(); // Add .change() here
    });
    

    或者将回调更改为实际函数并调用它:

    function marcaChange(){
        var marca = $("select#marca option:selected").attr('value');
        $("select#modello").html(attendere);
    $.post("select.php", {id_marca:marca}, function(data){
            $("select#modello").html(data);
        });
    }
    
    $(function() { 
        $("select#marca").change(marcaChange);
        marcaChange();
    });
    

    【讨论】:

      【解决方案4】:

      要调用onload,可以试试jQuery:

       $(document).ready(function(){
       onchange();// onload it will call the function
           });
      

      像这样编写你的 onchange 函数,这样会在onchange 发生时调用,

      function onchange(){
          var marca = $("select#marca option:selected").attr('value');
          $("select#modello").html(attendere);
          $.post("select.php", {id_marca:marca}, function(data){
          $("select#modello").html(data);
      });
      

      【讨论】:

        【解决方案5】:

        这对我有用。

         $(function () {
            $('#checkboxId').on('change', hideShowfunction).trigger('change');
        });
        
        function hideShowfunction(){
        //handle your checkbox check/uncheck logic here
        }
        

        【讨论】:

          【解决方案6】:

          另一种方法

           $("select#marca").val('your_value').trigger('change');
          

          【讨论】:

            最近更新 更多