简体   繁体   English

如何告诉程序刷新元素的值

[英]how to tell the program the value of an element is refreshed

How can refresh an html element when its value is changing. 如何在值变化时刷新html元素。 I mean assume that i have this tag: 我的意思是假设我有这个标签:

<div id="response" style="display:none;"><div>

and i have this script: 我有这个脚本:

<script type="text/javascript">
    $('#delete_link').click(function(e) {
        e.preventDefault();
        $('#result').html('').load('{% url messages_delete message.id %})
        alert($('#result').html());
        $('#'+$('#result').html()+'_list').trigger('click');
    });
</script>

when user clicks on delete_link, the server sends a value to response div, so response div which was empty before, now has a value, and i will use it's value to know which tab i should trigger. 当用户点击delete_link时,服务器会向响应div发送一个值,所以之前为空的响应div现在有一个值,我将使用它的值来知道我应该触发哪个选项卡。 when i use alert, it understands that the value of this tag is changed, and the trigger works. 当我使用alert时,它知道此标签的值已更改,并且触发器有效。 but without alert, it seems it doesn't understand the value is changed and trigger doesn't work. 但没有警报,似乎它不理解值被更改并且触发器不起作用。 Is there a way to tell the program that the value is changed? 有没有办法告诉程序值是否已更改? i don't want to use alert. 我不想使用警报。 thanks very much for your help 非常感谢您的帮助

.Load() is asynchronous, you need to set your content html in the load callback function. .Load()是异步的,你需要在加载回调函数中设置你的内容html。

 $('#delete_link').click(function(e) {
        e.preventDefault();
        $('#result').html('').load('{% url messages_delete message.id %}',function(){
           $('#'+$('#result').html()+'_list').trigger('click');
        });
    });

AJAX is asynchronous, you can use the load callback function which is executed after request is complete. AJAX是异步的,您可以使用在请求完成后执行的加载回调函数。

$('#delete_link').click(function(e) {
    e.preventDefault();
    $('#result').load('url', function(){
       $('#'+ $(this).html() +'_list').click()
    })
});

Note that load before adding new markup, removes the current markup of the element, so using html('') is redundant. 请注意,在添加新标记之前加载会删除元素的当前标记,因此使用html('')是多余的。

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

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