2013-02-20 23 views
22

मेरे पास एक ऐसा फ़ंक्शन है जो परिवर्तन ईवेंट पर पोस्ट क्रियाएं चलाता है।jquery ऑनलोड पर निष्पादन ईवेंट निष्पादित करें

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

मैं इस फ़ंक्शन ऑनलोड ईवेंट को निष्पादित करना चाहता हूं। क्या यह संभव है? क्या ऐसा करने का कोई अच्छा तरीका है?

+0

आप लोड पर समारोह को गति प्रदान मतलब है? क्या आपने कोशिश की है: $ ("# मार्का चुनें")। बदलें(); – cernunnos

उत्तर

42

बस इसे डाल एक फ़ंक्शन में, फिर इसे दस्तावेज़ पर भी कॉल करें, जैसे:

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

मैंने $ (विंडो) .load (function() { $ (फ़ंक्शन() { $ ("# मार्का चुनें") बदलें। (बदलें; }); लेकिन घटना नहीं चलती .. –

+1

@PaoloRossi ' $ (फ़ंक्शन() '** पहले से ही पृष्ठ लोड पर निष्पादित होगा ** इसे '$ (विंडो) .load' के अंदर लपेटें और सुनिश्चित करें कि आपने' बदलने 'की कोशिश करने से पहले' परिवर्तन 'ईवेंट पंजीकृत कर लिया है। – mattytommo

+0

बिल्कुल सही! बहुत बहुत धन्यवाद! –

14

आप अंत में यह बाध्य किए जाने के बाद सीधे बुलाया जाएगा करने के लिए .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(); 
}); 
14
यह सिर्फ इस तरह अपने परिवर्तन पर समारोह के अंत में एक और .change() घटना श्रृंखला के लिए वास्तव में आसान तरीका

:

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

अन्य तरीके से करना

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

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