簡體   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