将此原型代码转换为jquery代码?

时间:2011-04-04 19:58:52

标签: jquery prototypejs

我有这个基于原型的javascript代码。现在我想将其转换为jQuery代码。

 <script language="javascript" type="text/javascript">
 var submitcount=0;//prevent double click
 function segmentForm2(){

      with (document.formTest) {
           var file = segment.value;
           alert("testing: "+file);
           var pars = 'roomtype=<?php echo $_POST['roomtype']; ?>&etc';
           if((file != '') && (submitcount == 0)) {
                //alert('thank you' +submitcount);
                new Ajax.Updater('formfields', file, { method: 'post', parameters: pars});
                new Effect.Appear('formfields', {duration: 0.2});
        // submitcount++;
           } else {
                var file = 'blank.htm';
                new Ajax.Request('formfields',file, {method: 'get'});
                document.getElementById('formfields').style.display = 'none';
           }
      }

 }
 var myGlobalHandlers = {
      onCreate: function(){
           Element.show('systemWorking');
      },
      onComplete: function() {
           if(Ajax.activeRequestCount == 0){
                Element.hide('systemWorking');
           }
      }
 };
 Ajax.Responders.register(myGlobalHandlers);
 </script>

使用以下HTML代码

 <form name="formTest" method="POST">
      <select name="segment" id="segment"  onchange="javascript:segmentForm2();">
           <option value="">Segment</option>
           <option value="luxury.php">Luxury</option>
           <option value="nonluxury.php">Non luxury</option>
      </select>
      <div id="formfields" style="display:none;"></div>
      <div id="systemWorking" style="display:none"><img src="imgs/ajax-loader.gif" alt="Loading" /></div>
 </form>

1 个答案:

答案 0 :(得分:2)

这应该这样做。干杯。

var file = segment.value;     
alert("testing: "+file);     
var pars = { roomtype : '<?php echo $_POST['roomtype']; ?>'};    
if((file != '') && (submitcount == 0)) 
{         
//alert('thank you' +submitcount);   
$.post(file, pars, function(data) { 
    $('#formfields').html(data).show(2000); 
});      
//new Ajax.Updater('formfields', file, { method: 'post', parameters: pars});  
//new Effect.Appear('formfields', {duration: 0.2}); 
// submitcount++;       
} else {         
var file = 'blank.htm';
$('#formfields').load(file).hide();         
//new Ajax.Request('formfields',file, {method: 'get'});         
//document.getElementById('formfields').style.display = 'none';     
}