Javascript Jquery 在 onload 上执行 onchange 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14984598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:34:08  来源:igfitidea点击:

Jquery execute onchange event on onload

javascriptjquery

提问by Paolo Rossi

I have a function that on change event run the post actions.

我有一个功能,在更改事件时运行发布操作。

$("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);
    });
});

I would like to perform this function onload event. Is it possible? Is there a good way to do this?

我想执行这个函数 onload 事件。是否可以?有没有好的方法可以做到这一点?

回答by mattytommo

Just put it in a function, then call it on document ready too, like so:

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

$(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);
    });
}

Or just invoke changeon page load?

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

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

回答by stevecomrie

Really simple way it to just chain another .change() event to the end of your on change function like this:

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

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

回答by Richard Dalton

If you add .change()to the end it will be called straight away after being bound:

如果添加.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
});

Or change the callback to an actual function and call that:

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

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();
});

回答by Vigneswaran S

To call onload, you can try jQuery:

要调用onload,您可以尝试 jQuery:

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

Write your onchange function like this, so this will be called when onchangeoccurs,

像这样编写您的 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);
});

回答by Jaydeep Shil

this is working for me.

这对我有用。

 $(function () {
    $('#checkboxId').on('change', hideShowfunction).trigger('change');
});

function hideShowfunction(){
//handle your checkbox check/uncheck logic here
}

回答by Dushyant Joshi

The other way to do

另一种做法

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