2011-04-01 40 views
0

我想檢查複選框時調用html頁面。我嘗試了以下jQuery代碼,但它不起作用。我犯了什麼錯誤?如何使用jQuery調用複選框更改的html頁面?

<script type="text/javascript"> 
     $('#my_checkbox').change(function() { 
     alert('Handler for .change() called.'); 
     $.get("equation_section.html"); 
     }); 
</script> 

這是我的複選框:

<input class="checkbox" type="checkbox" name="my_checkbox" id="my_checkbox"/> 

編輯

$('#my_checkbox').change(function() { 
if ($('#my_checkbox').attr("checked") == true) { 
    alert('Handler for .change() called.'); 
     $('#my_checkbox').addClass('jQlightbox'); 
      $.get('equation_section.html', function(data) { 
       $('.equation_section').html(data); 
     }); 
    } 
}); 

這是我編輯的代碼。現在我收到警報框。該頁面也被加載到div中。如果我只想將div內容顯示在模式窗口上而不是之前的頁面內容怎麼辦?有人建議我。

+0

「被叫」是什麼意思? – sdleihssirhc 2011-04-01 05:38:55

+0

我想要加載其他頁面,即方程section.html。 – Angeline 2011-04-01 05:41:12

回答

3

您是(可能)獲得的頁面,但後來什麼都不做吧。很明顯,你應該看看jQuery.get() page

的示例代碼此外,你是否意識到這將基本上每當複選框被點擊時調用?如果你想在 「equation_section.html」 頁面加載到一個div,你可以做到以下幾點:

在頁面:

<div id="equation_section"></div> 

的Javascript:

<script type="text/javascript"> 
// on DOM load 
$(function() { 

    $('#my_checkbox').change(function() { 
    alert('Handler for .change() called.'); 
    $.get('equation_section.html', function(data) { 
     $('#equation_section').html(data); 
    }); 
    }); 

}); 
</script> 

如果你是試圖將用戶重定向到其他頁面,您應該這樣做:location.href = "equation_section.html";

+0

無論如何,我甚至沒有彈出提示框.. – Angeline 2011-04-01 05:48:39

+0

請確保您使用'$(function(){});'在DOM加載後執行代碼。 – JustinStolle 2011-04-01 06:02:37

+0

,這意味着變化事件沒有被觸發? – Angeline 2011-04-01 06:02:52

1

你想讓你的頁面位置改變嗎?如果是這樣,你可以使用window.location = "equation_section.html"。否則,你必須設置一個DOM元素的html。

HTML

<div id="test_div"></div> 

的JavaScript

$("#test_div").load("equation_section.html"); 
+0

謝謝。如果我只想顯示div內容而不是上一頁的內容怎麼辦? – Angeline 2011-04-01 07:11:04

相關問題