在JavaScript设置剃须刀变量 [英] Setting razor variables in javascript

查看:99
本文介绍了在JavaScript设置剃须刀变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的窗体上组合框的内容剃刀变量从javascript函数内分配。像这样:

I am trying to assign a razor variable with the contents of a combo box on my form from within a javascript function. Like so:

<script type ="text/javascript">
<!--
    function SomeFunction() {

        var hours = document.getElementById("Hours");
        var task = document.getElementById("TaskType_ID");
        @{            
            var tsk = @:task.value;            
         }

        @{            
        <text>
            hours.value = '@ViewBag.TaskTypes.GetHours(tsk)';
        </text>
        }

        return true;
    }

//-->
</script>

行(和所有其他的变化我试过)

the line (and every other variation I've tried)

var tsk = @:task.value;

导致错误。

推荐答案

由于克里斯指出的那样,你是混合客户端和服务器code。 JavaScript的是客户端code和从服务器需要的数据通过使用AJAX调用函数(GET / POST)返回所需的值。这里是你可以做什么的片段code:

As Chris pointed out, you are mixing the client and server code. The javascript is the client code, and you need the data from the server to return the value you want by using ajax calling function(get/post). Here is the snippet code of what you can do about it:

$("#tasks").change(function(){
    $.get('url',$(this).val(), function(data){
        // data is the hours returned from the selected task
        $("#hours").val(data);
    });
});

或代替打电话给你选择任务的服务器每次,你可以随时任务的值存储在一个变量。谢谢

Or instead of making calls to the server everytime you select a task, you can always store the values of the tasks in a variable. Thanks

这篇关于在JavaScript设置剃须刀变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆