채택완료

function 호출 질문드립니다.

2022-05-31 (화) 13:47:30 1,633
Copy
$(document).on("change", ".select", function(e) {

     ajax_test();

});

 

function ajax_test() {

       $.ajax({
        url : "./search.php",
        type : "post",
        data : {
            test_value : $(this).closest('td').find(".insVal").val()
        },

..

..

}

ajax_test() 를 만들어서 다른 select 에서도 사용하고 싶습니다.

ajax_test의 내용이 $(document).on("change", ".select", function(e) { 안에 있을때는 

정상적으로 작동이 하는데

function 으로 빼면 작동이 안됩니다.

아마.. $(this).closest('td').find(".insVal").val() 를 못가져오는 듯합니다.

해당 위치값을 fuction 으로 넘길려면 어떻게 수정해야 할까요?

|

답변 3개 / 댓글 2개

채택된 답변
+20 포인트
$(document).on("change", ".select", function(e) {
     var select_val = $(this);
     ajax_test(select_val);
});
function ajax_test(test) {
       $.ajax({
        url : "./search.php",
        type : "post",
        data : {
            test_value : test.closest('td').find(".insVal").val()
        },

이렇게하면 되나요?

답변에 대한 댓글 1개

답변 감사합니다.
2022-05-31 (화) 14:26:37

적어주신 소스 기준으로

Copy
$(document).on("change", ".select", function(e) {
     var select_val = $(this);
     ajax_test(select_val);
});

select_val 에 변수를 담아서 ajax_test function 을 호출하시고

Copy
function ajax_test(select_val) {
       $.ajax({
        url : "./search.php",
        type : "post",
        data : {
            test_value : select_val
        },

이런식으로 받아서 처리하시면 됩니다

답변에 대한 댓글 1개

답변 감사합니다.

ajax_test에서 해당 인자를 받으셔야 합니다
ajax_test($(this))
function ajax_test(target) {
...
test_value: target.closest...

답변을 작성하려면 로그인이 필요합니다.