将JavaScript变量剃刀code之内 [英] Place Javascript Variable within Razor Code

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

问题描述

我有一个JavaScript函数添加了一行到表在文本框中选择的值。但是,我怎么会选定的值放置在剃刀code里面呢?

  VAR VAL =的document.getElementById(标识)的价值;
VAR strHtml2 ='@ Html.TextBoxFor(X => x.extraDoc.dr_name,新{值=瓦尔地在这里,@class =输入1,@ReadOnly =真});


解决方案

没有是不可能的,因为.NET MVC是服务器端的code和被发送到客户端之前进行评估,而JavaScript是客户端$ C $ç只运行一次,它是在客户端。

其实你可以做一件事,你可以通过使用获得该元素的标识

  @ Html.TextBoxFor(X => x.extraDoc.dr_name,新{值=瓦尔地在这里,@class =输入1,@ReadOnly =真})

剃刀将生成ID字段的TextBoxFor可能ID将是这样的: extraDoc_dr_name (或),你可以在浏览器开发人员工具找到它

然后就可以使用JavaScript设置:

 的document.getElementById(extraDoc_dr_name)值=的document.getElementById(标识)的价值。

I have one JavaScript function which adds a row into the table with the selected value in a text box. But how will i place the selected value inside the razor code ?

var val = document.getElementById("tags").value;
var strHtml2 = '@Html.TextBoxFor(x => x.extraDoc.dr_name, new {Value="Place Val Here", @class = "input1", @readonly = true })';

解决方案

No it is not possible, because .NET MVC is server side code and is evaluated before being sent to the client, and javascript is Client side code which runs only once it is ON the client.

Actually you can do one thing you can get the element by using Id.

@Html.TextBoxFor(x => x.extraDoc.dr_name, new {Value="Place Val Here", @class = "input1", @readonly = true })

Razor will generate id field for the TextBoxFor probably id will be something like this: extraDoc_dr_name (or) you can find it in browser developer tool.

then you can set it using javascript:

document.getElementById("extraDoc_dr_name").value = document.getElementById("tags").value;

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

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