简体   繁体   English

在 asp.net 内核中使用 javascript/jquery 刷新 razor 页面而不重新加载

[英]Refresh razor page without reload using javascript/ jquery in asp.net core

I have one input and script to reload table in my page.我有一个输入和脚本来在我的页面中重新加载表格。 In the input if I insert 1000 I want to reload page every second, without clicking any buttons.在输入中,如果我插入 1000 我想每秒重新加载页面,而不单击任何按钮。

This is what I tried:这是我尝试过的:

<input id="txtRefresh" />

<script>
    document.redy(function () {
        $('tblRefresh').load();
        setInterval(function () {
            $('#tblRefresh').load();
        }, 'txtRefresh');
    });
</script>

Its cshtml razor page.它的cshtml razor页面。

What I want to do is to insert the value of seconds in the input andrefresh the page based of the of the inserted value, without submiting any data.我想要做的是在input中插入秒的值并根据插入的值刷新页面,而不提交任何数据。

Is it possible to do this?是否有可能做到这一点? Any suggestions?有什么建议么?

Thanks in advance!提前致谢!


UPDATE更新

I inserted this script:我插入了这个脚本:

<script>
    document.ready(function () {
        $('#refreshDIV').load('url/url/url');
        setInterval(function () {
            $('#refreshDIV').load('url/url/url');
        }, $('#txtRefresh').val());
    });
</script>

But it gaves me error!但它给了我错误!

refreshDIV is a div that I want to refresh refreshDIV是我要刷新的 div

txtRefresh is the input from I insert the seconds txtRefresh是我插入秒的输入

you can try this:你可以试试这个:

$(document).ready(function(){
     var timeout = $("#txtRefresh").val();
     setTimeout(timeout, function() { location.href=""; });
});

but each time you will reload the page you will lose, the input.但是每次您重新加载页面时,您都会丢失输入。 You could send it through querystring like this:您可以像这样通过查询字符串发送它:

location.href="?timeout="+timeout

and populate the input server side并填充输入服务器端

This is just a string:这只是一个字符串:

'txtRefresh'

If you want to get the value of that element , it would be more like this:如果您想获取该元素的值,则更像是这样:

$('#txtRefresh').val()

You also have a typo in document.ready and in your first #tblRefresh selector (missing the # ), and you aren't supplying .load() with the URL you want to load .您在document.ready您的第一个#tblRefresh选择器中也有错字(缺少# ),并且您没有为 .load .load()提供要加载的 URL All together you appear to be trying to do this:您似乎都在尝试这样做:

$(document).ready(function () {
    $('#tblRefresh').load('/some/url');
    setInterval(function () {
        $('#tblRefresh').load('/some/url');
    }, $('#txtRefresh').val());
});

A couple things to note:有几点需要注意:

  1. The value /some/url is of course a placeholder in this sample code. /some/url值当然是此示例代码中的占位符。 Whatever URL you want to use in your application is up to you.无论您想在应用程序中使用什么 URL 都取决于您。

  2. Calling .load() will place the entire response of that URL into the target element.调用.load()会将 URL 的整个响应放入目标元素中。 If you want only a subset of that response, you can add selectors to .load() .如果您只想要该响应的一个子集,则可以将选择器添加到.load() For example:例如:

    $('#tblRefresh').load('/some/url #someElement');

This would tell .load() to grab all the content from /some/url and then only select from it the content of #someElement to place in #tblRefresh .这将告诉.load()/some/url中获取所有内容,然后仅从 select 中获取#someElement的内容以放置在#tblRefresh中。 The performance could still potentially be slow as all of the content from the URL is still downloaded.由于 URL 中的所有内容仍在下载中,因此性能可能仍然很慢。 To address performance or for finer control over data, consider returning JSON data from the server and using .ajax() to fetch that data instead of using .load() to reload all of the HTML (when only the data has changed).为了提高性能或更好地控制数据,请考虑从服务器返回 JSON 数据并使用.ajax()获取该数据,而不是使用.load()重新加载所有 HTML(仅当数据发生更改时)。

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

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