@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
Func<int,int,int> Sum = (a, b) => a + b;
}
//inside table
<td>@Sum(3,4)</td>
这会输出正确的答案,虽然我想在一个可以调整的文本框内输出(这样数据可以回发)......我的尝试....
<td><input id="Name" name="Name" type="text" value="">
@Minus(@products.ReorderLevel, @products.StockLevel)
</input>
</td>
说输入元素是空的,不能有结束标记。
理想情况下,我想在文本框'+'%' - '后面有2个小按钮,这会在单击时增加或减少文本框中的值....?
答案 0 :(得分:2)
使用'value'属性设置文本输入字段的值
<td><input id="Name" name="Name" type="text"
value="@Minus(products.ReorderLevel, products.StockLevel)" />
</td>
要更改值,您必须编写一些JavaScript。查看jquery以查找和操作DOM对象的简单方法,例如文本框($("..")
- 下面示例中的内容
<script type="text/JavaScript" src="/path/to/your/jquery.version.js"></script>
<script type="text/JavaScript">
// Declare a function to increment a value
var incrementField = function()
{
var newValue = 1 + parseInt($("#name").val());
$("#name").val(newValue);
};
// Declare a function to decrement the value
var decrementField = function()
{
var newValue = parseInt($("#name").val()) - 1;
$("#name").val(newValue);
};
</script>
并从你的html调用它:
<button onclick="incrementField()">+</button>
<button onclick="decrementField()">-</button>
这是非常基本的,未经测试和原型质量的东西。另一种方法是使用jQuery .click()来连接增加/减少逻辑。
更新:在此处运行jsFiddle示例: http://jsfiddle.net/Am8Lp/2/
为您的按钮设置ID并使用以下javascript:
// This creates a callback which called when the page is fully loaded
$(document).ready(function(){
// Set the initial value of the textbox
$("#name").val('0');
// Create a click handler for your increment button
$("#increaseButton").click(function(){
var newValue = 1 + parseInt($("#name").val());
$("#name").val(newValue);
});
// .. and your decrement button
$("#decreaseButton").click(function(){
var newValue = parseInt($("#name").val()) - 1;
$("#name").val(newValue);
});
});
最后为您的按钮添加ID并删除旧的点击处理程序
<button id="increaseButton">+</button>
<button id="decreaseButton">-</button>