我有以下形式檢索模型屬性值
@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>
}
然後辦法得到這個模特屬性in 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
感激,如果能提出好辦法駿馬出
哪些錯誤只用'$( '#testid')點擊(函數(){警報($( '#PRODUCT_ID')VAL());}'但什麼是'ID的元素=「testid」'? –
@StephenMuecke這樣的方式,但有沒有任何函數來獲得該值沒有'.click()'? – kez
不知道你想達到什麼 - 你可以例如使用'$(「#Product_ID 「).change(function(){alert($(this).val());})'每次用戶更改該值時顯示警報(並且退出控件) –