2012-03-08 60 views
1
$('test_div').addEvent('click', function() { 
     formElement = $$('.field_search_criteria')[0]; // get it from <form class="field_search_criteria" id="field_search"> 

     // On search 
     formElement.addEvent('submit', function(event) { 
      event.stop(); 

     alert(formElement.toQueryString()) 
     }); 
    }); 

任何人都可以幫助我將上面的mootools函數轉換成Jquery。什麼是的addEvent在MooTools的替代Jquery的提前:)MooTools addEvent在Jquery中的替代方法

+0

您確實知道原始代碼會將'formElement'泄漏到您的全局對象中,對嗎?也可以寫成'document.getElement('。field_searchCriteria')'。 – 2012-03-08 15:42:43

回答

0

您可以使用上()

//When i click on the element with id = test_div (substitue # with . if you are talking about a class) 
$('#test_div').on('click', function() { 
    //select the form with id = field_search 
    formElement = $('#field_search'); // get it from <form class="field_search_criteria" id="field_search"> 

    // On Submit 
    formElement.on('submit', function(event) { 
     //prevent default action 
     event.preventDefault(); 
     //alert serialized data 
     alert(formElement.serialize()) 
    }); 
}); 
0

你也可以做

感謝:

 

jQuery.fn.addEvent = jQuery.fn.bind; 

//Usage 
$(document).ready(function() { 
    $('test_div').addEvent('submit',function(){ .... }); 
}); 
 
+0

是的,但他仍然需要更改所有其他代碼才能在jQuery中工作 – 2012-03-08 12:03:56