使用jquery/ajax刷新/重新加载Div中的内容

UID*_*UID 70 html javascript css ajax jquery

我想点击一个按钮重新加载一个div.我不想重新加载整页.

这是我的代码:

HTML:

<div role="button" class="marginTop50 marginBottom">
    <input type="button" id="getCameraSerialNumbers" value="Capture Again" class="disabled" />
    <input type="button" id="confirmNext"  value="Confirm & Proceed" class="disabled marginLeft50" />
</div>
Run Code Online (Sandbox Code Playgroud)

单击<input type="button" id="getCameraSerialNumbers" value="Capture Again">按钮时,<div id="list">....</div>应重新加载而不加载或刷新整页.

下面是我试过的Jquery,但没有工作: -

$("#getCameraSerialNumbers").click(function () {
    $("#step1Content").load();
});
Run Code Online (Sandbox Code Playgroud)

请建议.

这是我页面上的DIV,其中包含一些产品的图片和序列号......这将是第一次在Page Load上的数据库.但是,如果用户面临一些问题,他将点击"再次捕获"按钮" <input type="button" id="getCameraSerialNumbers" value="Capture Again">",这将再次加载这些信息.

HTML代码: -

<div id="step1Content" role="Step1ShowCameraCaptures" class="marginLeft">

<form>
    <h1>Camera Configuration</h1>
    <!-- Step 1.1 - Image Captures Confirmation-->
    <div id="list">
        <div>
            <p>
                <a id="pickheadImageLightBox" data-lightbox="image-1" title="" href="">
                    <img alt="" id="pickheadImage" src="" width="250" height="200" />
                </a>
            </p>
            <p>
                <strong>Pickhead Camera Serial No:</strong><br />
                <span id="pickheadImageDetails"></span>
            </p>
        </div>
        <div>
            <p>
                <a id="processingStationSideImageLightBox" data-lightbox="image-1" title="" href="">
                    <img alt="" id="processingStationSideImage" src="" width="250" height="200" />
                </a>
            </p>
            <p>
                <strong>Processing Station Top Camera Serial No:</strong><br />
                <span id="processingStationSideImageDetails"></span>
            </p>
        </div>
        <div>
            <p>
                <a id="processingStationTopImageLightBox" data-lightbox="image-1" title="" href="">
                    <img alt="" id="processingStationTopImage" src="" width="250" height="200" />
                </a>
            </p>
            <p>
                <strong>Processing Station Side Camera Serial No:</strong><br />
                <span id="processingStationTopImageDetails"></span>
            </p>
        </div>
        <div>
            <p>
                <a id="cardScanImageLightBox" data-lightbox="image-1" title="" href="">
                    <img alt="" id="cardScanImage" src="" width="250" height="200" />
                </a>
            </p>
            <p>
                <strong>Card Scan Camera Serial No:</strong><br />
                <span id="cardScanImageDetails"></span>
            </p>

        </div>
    </div>
    <div class="clearall"></div>

    <div class="marginTop50">
        <p><input type="radio" name="radio1" id="optionYes" />Yes, the infomation captured is correct.</p>
        <p><input type="radio" name="radio1" id="optionNo" />No, Please capture again.</p>
    </div>
    <div role="button" class="marginTop50 marginBottom">
        <input type="button" id="getCameraSerialNumbers" value="Capture Again" class="disabled" />
        <input type="button" id="confirmNext"  value="Confirm & Proceed" class="disabled marginLeft50" />
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

现在点击<input type="button" id="getCameraSerialNumbers" value="Capture Again" class="disabled" />按钮,<div id="list">... </div> 应该再次加载所在的信息.

如果您需要更多信息,请与我们联系.

Pec*_*eca 109

$("#mydiv").load(location.href + " #mydiv");
Run Code Online (Sandbox Code Playgroud)

  • 对于每次加载调用,这会在#mydiv中嵌入#mydiv.Juan提供了正确的版本. (8认同)
  • 它不一定是AJAX,但可以提供预期的用户体验 (2认同)

小智 53

$("#myDiv").load(location.href+" #myDiv>*","");
Run Code Online (Sandbox Code Playgroud)

  • @ php_coder_3809625说实话,Peca在Juan之后回答了几个月:) (21认同)
  • 请在答案中提供解释,这将有助于其他人理解为什么您认为这是一个很好的答案. (8认同)
  • @Juan:请你解释一下它到底做了什么.谢谢 (3认同)

Guc*_* Ca 40

我总是用这个,做得很完美.

$(document).ready(function(){
        $(function(){
        $('#ideal_form').submit(function(e){
                e.preventDefault();
                var form = $(this);
                var post_url = form.attr('action');
                var post_data = form.serialize();
                $('#loader3', form).html('<img src="../../images/ajax-loader.gif" />       Please wait...');
                $.ajax({
                    type: 'POST',
                    url: post_url, 
                    data: post_data,
                    success: function(msg) {
                        $(form).fadeOut(800, function(){
                            form.html(msg).fadeIn().delay(2000);

                        });
                    }
                });
            });
        });
         });
Run Code Online (Sandbox Code Playgroud)


Sac*_*ane 16

当这个方法执行时,它会检索内容location.href,然后jQuery解析返回的文档以找到带有的元素divId.此元素及其内容将插入到具有divId结果ID()的元素中,并且将丢弃检索到的文档的其余部分.

$("#divId").load(location.href +"#divivd**","");

希望这可以帮助别人理解


小智 7

你想要的是再次加载数据但不重新加载div.

您需要进行Ajax查询以从服务器获取数据并填充DIV.

http://api.jquery.com/jQuery.ajax/


Dav*_*d L 7

虽然您没有提供足够的信息来实际指示您应该从哪里提取数据,但您需要从某处提取数据.您可以在加载中指定URL,以及定义数据参数或回调函数.

$("#getCameraSerialNumbers").click(function () {
    $("#step1Content").load('YourUrl');
});
Run Code Online (Sandbox Code Playgroud)

http://api.jquery.com/load/


dau*_*lat 5

试试这个

HTML代码

 <div id="refresh">
    <input type="text" />
    <input type="button" id="click" />
 </div>
Run Code Online (Sandbox Code Playgroud)

jQuery代码

 <script>
    $('#click').click(function(){
    var div=$('#refresh').html();
    $.ajax({
        url: '/path/to/file.php',
        type: 'POST',
        dataType: 'json',
        data: {param1: 'value1'},
    })
    .done(function(data) {
if(data.success=='ok'){
        $('#refresh').html(div);
}else{
// show errors.
}
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
    });

</script>
Run Code Online (Sandbox Code Playgroud)

php页面代码路径=/path/to/file.php

<?php
   header('Content-Type: application/json');
   $done=true;
   if($done){
       echo json_encode(['success'=>'ok']);
   }
?>
Run Code Online (Sandbox Code Playgroud)