检索模型属性值
retrieve the model property value
我有以下表单
@model project_name.Models.AddNewProduct
<h4>Add New Product</h4>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.LabelFor(model => model.Product_ID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Product_ID, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
我想使用jquery或JavaScript获取此模型属性
为此,我只是按照下面的方法
@model project_name.Models.AddNewProduct
<h4>Add New Product</h4>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.LabelFor(model => model.Product_ID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Product_ID, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
<div id="testid" class="col-md-offset-2 col-md-10" >
<input type="button" value="Test" class="btn btn-default" />
</div>
</div>
}
然后在javascipt 中
<script type="text/javascript">
$(document).ready(function () {
var sampleID;
$("#Product_ID").keyup(function () {
sampleID = $('#Product_ID').val();
});
$('#testid').click(function () {
alert(sampleID);
});
});
</script>
但这似乎不是我的最佳方法,keyup
如果能在课程结束后提出一个获得这些值的好方法,请感激
您可以使用$('#Product_ID').val()
在#testid
的点击事件中获得#Product_ID
的值,如下所示。
$('#testid').click(function () {
alert($('#Product_ID').val());
});
由于您使用的是剃须刀,因此您可以使用@直接在jquery中获取它,如下所示:
$(function() {
var sampleID = "@Model.Product_ID";
});
该模型仅在服务器端。如果您需要对视图中的值执行某些操作,则可以将其处理为普通HTML页面和javascript。
如果要获取输入字段的值,请使用焦点丢失时触发的模糊事件。
$("#Product_ID").blur(function() {
alert( "Handler for .blur() called." );
});
它看起来像是JS中的一个拼写错误。把kepup改成keyup。
这应该如预期的那样起作用。
$("#Product_ID").keyup(function () {
sampleID = $('#Product_ID').val();alert(sampleID);
//or you can use
alert($(this).val());
});
相关文章:
- 使用导航属性创建Kendo UI网格模型的问题
- 将输入值设置为ng模型属性[Angular]时出现问题
- 如何使用Undercore和Backbone在HTML中表示模型属性
- 主干中的模型属性未定义
- ng重复中的ng模型-初始化唯一作用域属性
- sails.js beforeCreate方法只接收required设置为true的模型属性
- 如何使用javascript访问sails.js模型中的属性
- 如何更改与Backbone集合中的模型相关联的几个元素的css属性
- 从骨干集合筛选模型,然后为这些模型设置属性
- Ember.js:未在组件'中渲染的模型的计算属性;关于变更的参考
- 当ng repeat上的复选框具有必需的属性时,角度ng模型将变为未定义
- 将属性模型复制到剪贴板
- Backbone JS Promises在模型上设置属性之前解析
- 比较主干中的两个模型属性
- 在ember.js中访问模型之外的计算属性
- 如何在sails.js中更改属性模型中的日期格式
- 如何使用setVisible()方法设置json模型中元素的可见属性
- MVC6将视图模型列表属性分配给javascript变量
- 更新淘汰视图模型属性时激发更改事件
- angularJS + 猫鼬: 类型错误: 无法读取未定义的属性“模型”