【问题标题】:reload a div without reloading the whole page using ajax/jquery重新加载 div 而不使用 ajax/jquery 重新加载整个页面
【发布时间】:2014-09-06 13:14:03
【问题描述】:

我正在做一个项目,当用户完成同一页面中的表单时需要刷新。代码如下:

page1.jsp

 <div id="codeRefer">
    <input type="" type="text" name="numcode" id="numcode" value="42988715">
    <script language="javascript">
        var a = Math.ceil(Math.random() * 9) + '';
        var b = Math.ceil(Math.random() * 9) + '';
        var c = Math.ceil(Math.random() * 9) + '';
        var d = Math.ceil(Math.random() * 9) + '';
        var e = Math.ceil(Math.random() * 9) + '';
        var f = Math.ceil(Math.random() * 9) + '';
        var g = Math.ceil(Math.random() * 9) + '';
        var h = Math.ceil(Math.random() * 9) + '';
        var code = a + b + c + d + e + f + g + h;
        document.getElementById("numcode").value = code;

        function ValidCaptcha() {
            var str1 = removeSpaces(document.getElementById('numcode').value);
            var str2 = removeSpaces(document.getElementById('txtInput').value);
            if (str1 == str2) {
                return true;
                alert(str1);
            } else {
                return false;
            }
        }

        function removeSpaces(string) {
            return string.split(' ').join('');
        }
    </script>
</div>
<input type="button" value="REFRESH" onClick="javascript:refer()">
<script language="javascript">
    function refer() {
        $('#codeRefer').load(window.location.href + '#codeRefer');
    }
</script>

我得到的是,当我单击刷新时,文本框的值会被刷新,但是,我在第一个刷新按钮的底部得到另一个相同的“刷新”按钮。 我不知道为什么会出现另一个按钮。谢谢各位!

I think there is a bug in my code.
Which keeps repeating the whole content whenever I press the REFRESH button.
HELP me with this guys GURU'S.

【问题讨论】:

  • 你需要使用 iframe!
  • '工作小提琴将得到答复'。您希望有人为您制作小提琴,对吗?
  • { return true; alert(str1); } 看起来很不错
  • @Yury Tarabanko,是的,你是对的。请问你能做到吗?
  • 当您使用window.location.href + '#codeRefer' 时,您实际上是在 div 中加载整个页面。你只需要在 div 中加载#codeRefer 的内容。试试$('#codeRefer').html()

标签: javascript jquery html ajax jsp


【解决方案1】:

您可以尝试将所有脚本移出#codeRefer,并在加载html 之前调用empty()

像这样:

$('#codeRefer').empty().load(window.location.href + '#codeRefer');

编辑:如果您只想刷新 div,我建议您将要加载的内容作为模板放入,然后从该模板加载 html。示例:

<script type="text/template" id="refresh-template">
  <input type="" type="text" name="numcode" id="numcode" value="42988715">
</script>

以及您的处理程序中的 javascript 随机数生成:

function refer() {
    $('#codeRefer').empty().load(window.location.href + '#refresh-template');
    // OR if you are just loading from the same page. just use .html() might be better
    // $('#codeRefer').html($('#refresh-template').html());

    var a = Math.ceil(Math.random() * 9) + '';
    var b = Math.ceil(Math.random() * 9) + '';
    var c = Math.ceil(Math.random() * 9) + '';
    var d = Math.ceil(Math.random() * 9) + '';
    var e = Math.ceil(Math.random() * 9) + '';
    var f = Math.ceil(Math.random() * 9) + '';
    var g = Math.ceil(Math.random() * 9) + '';
    var h = Math.ceil(Math.random() * 9) + '';
    var code = a + b + c + d + e + f + g + h;
    document.getElementById("numcode").value = code;

}

http://jsfiddle.net/L7wgU/1/

【讨论】:

  • @Edward 我试过这个,但这和我在问题中提到的一样。代码再次使新的刷新按钮出现在第一个底部。
【解决方案2】:

试试这个:

function refer(ev) {
    $(ev.target).hide(); //hide the input button if want remove try .remove()
    var $currentCodeRefer = $("#codeRefer");
    $currentCodeRefer.ajax({
        url: window.location.href
    })
    .success(function(html) {
        var $newCodeRefer = $(html).find("#codeRefer");
        $newCodeRefer.empty().html($currentCodeRefer.html());
    });

//To receive the event data maybe need something like this:
$("buttonIDorClass").on("click", function(ev){refer(ev)});

不是更好的方法,但你需要,我建议有另一个像之前答案一样的 ajax,或者有一个 javascript 渲染并发送 json 数据并在客户端渲染。

【讨论】:

  • Quintana,我已经按照您的建议进行了尝试,但无法刷新代码。我在问题中编写的代码中有一个错误,这使得重复下面的文本框和按钮。 感谢您的回复
【解决方案3】:

这只能通过这种方式实现:

创建一个 pgae 说 ajax.html

<!-- ajax.html -->
<input type="" type="text" name="numcode" id="numcode" value="42988715">
<script language="javascript">
    var a = Math.ceil(Math.random() * 9) + '';
    var b = Math.ceil(Math.random() * 9) + '';
    var c = Math.ceil(Math.random() * 9) + '';
    var d = Math.ceil(Math.random() * 9) + '';
    var e = Math.ceil(Math.random() * 9) + '';
    var f = Math.ceil(Math.random() * 9) + '';
    var g = Math.ceil(Math.random() * 9) + '';
    var h = Math.ceil(Math.random() * 9) + '';
    var code = a + b + c + d + e + f + g + h;
    document.getElementById("numcode").value = code;

    function ValidCaptcha() {
        var str1 = removeSpaces(document.getElementById('numcode').value);
        var str2 = removeSpaces(document.getElementById('txtInput').value);
        if (str1 == str2) {
            return true;
            alert(str1);
        } else {
            return false;
        }
    }

    function removeSpaces(string) {
        return string.split(' ').join('');
    }
</script>

然后在主 html 文件中使用此代码,例如 index.html

<div id="codeRefer">

</div>
<input type="button" value="REFRESH" onclick="refer()">
<script language="javascript">
    function refer() {
        $('#codeRefer').load('ajax.html#codeRefer');
    }
    refer();
</script>

【讨论】:

  • @Waqar_Alamgir,感谢您的回复,但我不想要另一个页面来刷新代码,因为这只是大表单的一部分。我不能以这种方式使用它。谢谢你的尝试。
  • 还有其他方法可以只从 ajax 中获取 'numcode' 值吗?
  • 是的,您也可以使用 JSON。
  • 只使用html文件。在里面放一个“numcode”,然后使用 ajax。
【解决方案4】:

您可以使用 Ajax 表单提交和提交后清除字段。

【讨论】:

    猜你喜欢
    • 2013-09-18
    • 2016-04-19
    • 1970-01-01
    • 2013-10-28
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多