如何使用 jQuery 在更改复选框时调用 html 页面?
我希望在选中复选框时调用 html 页面。我尝试了以下 jQuery 代码,但它不起作用。我犯了什么错误?
<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 内容而不是上一页内容怎么办?请有人建议我。
I want a html page to be called when I check a checkbox. I tried the following jQuery code, but it isn't working. What is the mistake I have committed?
<script type="text/javascript">
$('#my_checkbox').change(function() {
alert('Handler for .change() called.');
$.get("equation_section.html");
});
</script>
And this is my checkbox:
<input class="checkbox" type="checkbox" name="my_checkbox" id="my_checkbox"/>
EDIT
$('#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);
});
}
});
This is my edited code. Now I get the alert box. And the page also gets loaded in the div. What if I want only the div content to be displayed upon the modal window and not the previous page content? Someone suggest me please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您(可能)获得了该页面,但随后不对其进行任何操作。显然,您应该查看 jQuery.get() 页面 中的示例代码
此外,执行您是否意识到每次单击复选框时都会调用此函数?如果您想将“equation_section.html”页面加载到 div 中,您可以执行以下操作:
在页面中:
Javascript:
如果您尝试将用户重定向到其他页面,您应该执行
location。 href = "equation_section.html";
You are (probably) getting the page, but then doing nothing with it. Obviously, you should be looking at sample code from the jQuery.get() page
Additionally, do you realize this will be called essentially every time the check box get clicked? If you want to load it the "equation_section.html" page into a div, you could do the following:
In the page:
Javascript:
If you are trying to redirect the user to the other page, you should do
location.href = "equation_section.html";
您想要更改页面的位置吗?如果是这样,您可以使用
window.location = "equation_section.html"
。否则你必须设置 DOM 元素的 html。html
javascript
Do you want the location of your page to change? If so you can use
window.location = "equation_section.html"
. Otherwise you'd have to set the html of a DOM element.html
javascript