繁体   English   中英

jQuery:从PHP文件中单击按钮时刷新div内容

[英]jQuery: refresh div content on button click, from a php file

小提琴: http//jsfiddle.net/feqnLs6o/

我正在尝试使用jQuery在button1单击上刷新<div id="refreshableDiv"> 我想每次单击button1时将http://shiro-desu.com/scr/test/test.php的内容加载到<div> 因为我以前没有使用过jQuery,所以我不明白为什么以下内容不会重新加载div的内容。

脚本代码:

$(document).ready(function(){
 $('#button1').click(function(){
        $.post(
        "http://shiro-desu.com/scr/test/test.php",
        {
        },
        function(data){
        $('#refreshableDiv').html(data);
        });
    });
});

test.php

<?php echo "543"; ?>

html代码:

<div id="refreshableDiv">Primary content</div>
<input type="submit" class="button1" name="button1" value="Reload"/>

有任何想法吗?

http://jsfiddle.net/feqnLs6o/

只需将按钮的html代码更改为:

<input type="submit" id="button1" name="button1" value="Reload"/>

绑定点击事件时,您正在使用ID。

看来您没有发布任何数据-改为执行AJAX GET请求更为合理。

$(document).ready(function(){
 $('.button1').click(function(){
     $.ajax({
         url:'http://shiro-desu.com/scr/test/test.php'
     }).done(function(response) {
        $('#refreshableDiv').html(response); 
     });
 });
});

检查控制台日志中的请求显示以下错误:

XMLHttpRequest无法加载http://shiro-desu.com/scr/test/test.php 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问源“ http://fiddle.jshell.net ”。 fiddle.jshell.net/_display/:1

推杆:

<?php header('Access-Control-Allow-Origin: *'); ?>

http://shiro-desu.com/scr/test/test.php的顶部应该可以解决该问题。

这是关于http://en.wikipedia.org/wiki/Same-origin_policy

如何刷新<div>在 angular 中单击按钮内容更快?</div><div id="text_translate"><p> 我有一个具有一定价值的内容,单击取消按钮时,我需要刷新它并显示新内容。 当我单击取消按钮时,旧内容会保留几秒钟,然后再更改为新内容。请告诉我如何更快地加载新内容而不会出现这种延迟。</p><pre> &lt;div&gt;Text&lt;/div&gt; &lt;button type="button" class="left-btn pull-right" (click)="reload()" data-dismiss="modal"&gt;cancel&lt;/button&gt; reload() { this.CommonService.GetDateTimeByTimeZone().subscribe(tz =&gt; { let ds: Input = { StartDate: moment(this.timezoneDate).format("YYYY-MM-DDT00:00:00.000[Z]"), EndDate: moment(this.timezoneDate).format("YYYY-MM-DDT23:59:59.999[Z]") } this.Dashboard(ds, false) }, error =&gt; { Swal.fire('', this.es.adminErrorMsgList[39].message, 'error') }); this.SelectedDate = ' '; }</pre></div>

[英]How to refresh <div> content faster on button click in angular?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM