2011-04-18 182 views
1
$(function() { 
    $('#cmd').bind('keydown', function (evt) { 
    if (evt.keyCode === 13) { 


//* I want to call the function here *// 
    } 
    }); 

}); 

這是我想用參數調用的函數。在jQuery中調用函數內部的外部函數

$(function (String msg) { 

var cmdStr = msg; 

    $.ajax({ 
     url: 'exec.php', 
     dataType: 'text', 
     data: { 
      q: cmdStr 
     }, 
     success: function (response) { 
      $('#txtOut').append(response); 
     } 

    }); 

} 
}); 

}); 

回答

2
$('#cmd').keydown(
    function (event){ 
     if(event.keyCode == 13){ 
     event.preventDefault(); 
     /*you can call your function here*/ 
     $.ajax({ 
      url: "exec.php", 
      method: "get", 
      dataType: "text", 
      data: { q: $('#com').val()}, 
      success: function(data){ 
      $('#txtOut').append(response); 
      } 
     }); 
     /*still you can it here*/ 
     } 
    } 
); 
2

將該功能置於文檔之外,併爲其命名。

function MyFunction(msg) 
{ 
var cmdStr = msg; 
$.ajax({ 
    url: 'exec.php', 
    dataType: 'text', 
    data: { 
     q: cmdStr 
    }, 
    success: function (response) { 
     $('#txtOut').append(response); 
    } 

}); 

} 

然後點擊事件稱之爲

$(function() { 
    $('#cmd').bind('keydown', function (evt) { 
    if (evt.keyCode === 13) {  

     MyFunction("message"); 
    } 
    }); 

});