简体   繁体   English

在Javascript中分配变量

[英]Assigning a variable in Javascript

I have defined index as <%var index = 0;%> This is at the top of the body of HTML so I can use it throughout as a global variable. 我已将index定义为<%var index = 0;%>它位于HTML主体的顶部,因此我可以将其始终用作全局变量。 and I want to assign value of $(this).val() to <% index %> . 并且我想将$(this).val()值分配给<% index %> I have tried multiple times but am not able to figure it out. 我已经尝试了多次,但无法弄清楚。 I am using Node.js in backend and EJS in front end. 我在后端使用Node.js,在前端使用EJS。

<script>
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                <% index %> = parseInt($(this).val()); ????
            }
            $modal.modal('show');
        }
    });
</script>

As someone mentioned you need to differentiate between what is calculated on the server and what is done on the browser: 正如有人提到的那样,您需要区分服务器上计算的内容和浏览器上完成的内容:

A common page might look like: 通用页面可能如下所示:

<script>
    var index = <% index_calculated_on_server %>;
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                index = parseInt($(this).val()); ????
            }
            $modal.modal('show');
        }
    });
</script>

Your problem lies in mixing server-side and client-side rendering. 您的问题在于混合服务器端渲染和客户端渲染。 To illustrate, let's take a look at the snippet you provided. 为了说明,让我们看一下您提供的代码段。

<script>
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                <% index %> = parseInt($(this).val()); // This is the problem line
            }
            $modal.modal('show');
        }
    });
</script>

EJS is rendered before even being sent to the client, merging in variables provided to it, so if you set index to be zero, you'll end up with a snippet like this after the rendering is done. EJS甚至在发送给客户端之前都会进行渲染,并合并提供给它的变量,因此,如果将index设置为零,则渲染完成后,您将得到一个类似这样的代码段。

<script>
    $("#agency").on("change", function () {
        var $modal = $('#myModal');
            if($(this).val() !== '0'){
                0 = parseInt($(this).val()); // Can't assign zero. 
            }
            $modal.modal('show');
        }
    });
</script>

Instead, what you should do is wrap your use of <% index %> in a wrapper element, which should allow you to manipulate it client side. 相反,您应该做的是将<% index %>的使用包装在wrapper元素中,这应该允许您在客户端进行操作。

 $("#agency").on("change", function () { var $modal = $('#myModal'); if( $(this).val() !== '0' ) { $(".index-counter").text(parseInt($(this).val())); } $modal.modal = function () { /* stub function */ }; $modal.modal('show'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" name="agency" id="agency" /> <!-- This would be where <% index %> was inserted --> <div class="index-counter">0</div> 

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

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