简体   繁体   English

jQuery / MVC3函数html onclick仅工作一次

[英]jQuery/MVC3 function html onclick only working once

Ok, i have a function that is called when in image is clicked. 好的,我有一个在单击图像时调用的函数。 For some reason it is only calling one time when i click it and I'm a little puzzled as to why. 由于某种原因,当我单击它时它只调用一次,而我对此有些疑惑。

Here's my javascript (combined with a small amount of razor), essentially all it does is call a post method in the controller that returns a partial view and updates that partial view. 这是我的javascript(结合少量剃刀),基本上它所做的就是在控制器中调用post方法,该方法返回部分视图并更新该部分视图。

function mFunction() {
    @{ x = x = 1; }
    $.post('/Home/_MyPartial', { 'param1': "@x" }, function(result) {
        $('.myUpdatedDiv').html(result);
        $('.anotherDiv').empty();
        $('.anotherDiv').append('@x');
    });
}

Then i have my tag like this 然后我有这样的标签

<a href="#" onclick="mFunction()">Hello</a>

It updates the view perfectly fine as i expect it to, with some goofy exceptions. 它可以像我期望的那样完美地更新视图,但有一些愚蠢的例外。

  1. It does hit the controller every time it is clicked (multiple times). 每次单击它都会击中控制器(多次)。 So i know it's working that way 所以我知道它是这样工作的
  2. The page itself does not get updated and nothing gets replaced a second time. 页面本身不会更新,并且第二次没有任何替换。 The controller successfully returns the partial view, but the javascript only updates it once. 控制器成功返回了部分视图,但是javascript只更新了一次。

So i'd appreciate any help as to why it isn't updating more than once. 因此,对于它为什么不会多次更新,我将不胜感激。

thanks 谢谢

edit 编辑

Ok lets say i do this: 好的,可以说我这样做:

@{ int x = 0; }

<script type="text/javascript">
function tester() {
    @{ x++; }
    $('.H').empty();
    $('.H').append('@x');
}
</script>

<div class="H"></div>
<a href="#" onclick="tester()">Click</a>

Clicking on the tag will display '1' and clicking it on it will always display '1'. 单击标签将显示“ 1”,并在标签上单击将始终显示“ 1”。 Does this have something to do with the razor code only rendering once or something? 这与仅一次渲染一次的剃刀代码有关吗? is there a way around it? 有没有解决的办法?

try this 尝试这个

<script type="text/javascript">
var indx = 1;//Set this to be the value from the model you want to display 
function tester() {
    $('.H').empty();
    $('.H').append(indx);
indx++;
}
</script>

<div class="H"></div>
<a href="#" onclick="tester()">Click</a>

Razor code is only run on the server, when the page has been downloaded all you have is the javascript with the Razor values output in place so your second example will look like this: 剃刀代码仅在服务器上运行,当页面下载完毕后,您所拥有的就是带有剃刀值输出的javascript,因此您的第二个示例将如下所示:

<script type="text/javascript">
function tester() {
    $('.H').empty();
    $('.H').append('1');
}
</script>

<div class="H"></div>
<a href="#" onclick="tester()">Click</a>

This is why it will only ever append 1. 这就是为什么它只会追加1。

I suggest you use Firefox's Firebug or the Web Console (Ctrl+Shift+K) to look at the asynchronous calls. 我建议您使用Firefox的Firebug或Web控制台(Ctrl + Shift + K)查看异步调用。 You'll know there if the browser is actually hitting the server on every click (and you can even see with what parameters whether POST or GET). 您将在这里知道浏览器是否真的在每次单击时都在访问服务器(甚至可以查看带有哪些参数,无论是POST还是GET)。 It looks like it should be contacting the server everytime, but because state is not kept between calls, you are always getting the same result after the 1st call (because you are always sending the same X value after the first call). 看起来它应该每次都与服务器联系,但是由于在两次调用之间不保持状态,因此在第一次调用之后您始终会得到相同的结果(因为在第一次调用之后始终发送相同的X值)。

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

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