2013-10-31 3 views
1

기본적으로 버튼을 클릭 할 때 전체 페이지를 다시로드하면서 경고를 생성하려고합니다. 내가 버튼을 클릭하면 내 컨트롤러에레일에서 버튼을 클릭 할 때 Ajax 호출

:javascript 
    function ajax_test1(field) 
    { 
     var xmlhttp; 
     if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp = new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     xmlhttp.open("GET", "test_ajax.xml?code=" + field.value, false); 
     xmlhttp.send(); 
     xmlDoc=xmlhttp.responseXML; 
     alert(xmlDoc.getElementsByTagName("message")[0].childNodes[0]); 
    } 

= button_to 'test ajax', :remote => true, :onclick => "ajax_test1(this);" 

def test_ajax 
    @test = {message:'Hello there!'} 
    render :xml => @test 
    end 

페이지는 다음 URL로 다시로드 : 내보기에

http://localhost:4242/test?onclick=ajax_test1%28this%29%3B&remote=true 

어떻게 해결할 수 있습니까?

답변

0

= button_to_function 'test ajax', 'ajax_test1(this)' 

하고 작업 :이에

= button_to 'test ajax', :remote => true, :onclick => "ajax_test1(this);" 

에드!

1

기본 동작 (실제 클릭이 발생하지 않도록)을 중지하려면 onclick 이벤트가 false을 반환해야합니다. 당신이 jQuery를 사용한 경우

.preventDefault() 방법은 기본 이벤트 동작을 방지하기위한 더 좋은 방법입니다 -하지만 당신의 손은 자바 스크립트 압연 당신은 조금 지저분해야합니다 :이 변경

= button_to 'test ajax', :remote => true, :onclick => "ajax_test1(this); return false;" 
+0

'return false; '를 추가했지만 같은 결과를 얻었습니다. – Pol0nium

관련 문제