2011-04-18 8 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"); 
    } 
    }); 

});