programing

jQuery: 앵커 href를 온클릭하여 비동기적으로 송신합니다.

lastcode 2023. 4. 2. 10:36
반응형

jQuery: 앵커 href를 온클릭하여 비동기적으로 송신합니다.

클라이언트측에서 하는 일은 거의 없고, 이 간단한 작업은 매우 귀찮습니다.

몇 가지 링크가 있습니다.OnClick 기본 액션을 방지하고 href URL을 캡처하고 해당 URL에 Ajax GET을 전송하고 단순하게alert()결과를 보면... 하지만 출발선도 통과하지 못하고 있습니다.

재생 시간의 앵커 예:

<a class="asynch_link" href="good/1.html">Click Here</a>
<a class="asynch_link" href="good/2.html">Click Here</a>

SO에 관한 유사한 요청에 대해 몇 가지 제안을 해 봤지만 링크를 통해 브라우저가 href URL로 이동할 수 있습니다.

단 하나라도

<script type="text/javascript">
    $('a').click(function (event) 
    { 
         event.preventDefault(); 
         //here you can also do all sort of things 
    });
</script>

...링크는 다른 페이지로 이동합니다.

여기 있으면 왠지 아기처럼 느껴져 :)

어떤 도움이라도 깊이 감사드립니다.

, jQuery를 포함합니다:)
<script src="//67.20.99.206/javascripts/jqueryCountdown/1-5-11/jquery.countdown.js" type="text/javascript" charset="utf-8"></script>

$('a').click(function(event) { 
    event.preventDefault(); 
    $.ajax({
        url: $(this).attr('href'),
        success: function(response) {
            alert(response);
        }
    });
    return false; // for good measure
});

이거 드셔보세요

$('a').click(function (event) 
{ 
   event.preventDefault(); 

   var url = $(this).attr('href');
   $.get(url, function(data) {
     alert(data);
    });

 });

여기서의 문제는 이벤트가 DOM ready 이벤트로 바인드되지 않기 때문에 해당 요소에 연결되어 있지 않다는 것입니다.DOM 준비 이벤트에 이벤트를 포함시켜 보십시오. 성공하면 경고가 표시됩니다.

<script> 
    $(function() {
         $('a').click(function(event) {
            event.preventDefault();
            alert('fff')
        //here you can also do all sort of things 
        });
    }); 
</script>

그 후 Ajax 요청을 전송하고 Success 콜백 함수로 폼을 전송합니다.

<script> 
    $(function() {
         $('a').click(function(event) {
             event.preventDefault();
             $.ajax({
                 url: 'url',
                 dataType :'json',
                 data : '{}',
                 success :  function(data){
                     // Your Code here

                     $('#form').submit();
                 }
             })
        });
    }); 
</script>

언급URL : https://stackoverflow.com/questions/12648916/jquery-capture-anchor-href-onclick-and-submit-asynchronously

반응형