如何保存要在服务器代码中使用的 JavaScript 值
how to save javascript values to use in server code?
我有一个标签,显示价格取决于通过javascript的其他2个DropDownList。我想在提交按钮命中时在我的服务器代码中使用此标签值。我怎么能弄清楚呢?
.aspx.cs页面代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text);
System.Windows.Forms.MessageBox.Show(totalPrice.ToString());
return;
}
.aspx页面代码:
<script type="text/javascript">
var ratesArray = [[0, 0], [1, 1000], [2, 1500], [3, 2000], [4, 2500], [5, 3000]];
var days = 7;
var pricePerDay = 0;
var TotalPrice;
function timleLimitDrpFunc() {
var tElem = document.getElementById('<%=drpTimeLimit.ClientID%>');
days = tElem.options[tElem.selectedIndex].value;
TotalPrice = days * pricePerDay;
SetPrice();
}
function ratesDrpFunc() {
var rElem = document.getElementById('<%=drpRates.ClientID%>');
var rateIndex = rElem.options[rElem.selectedIndex].value;
pricePerDay = ratesArray[rateIndex][1];
TotalPrice = days * pricePerDay;
SetPrice();
}
function SetPrice() {
var pElem = document.getElementById('<%=lblMablaghGhabelePardakht.ClientID%>');
pElem.innerHTML = TotalPrice;
pElem.value = TotalPrice;
}
</script>
<asp:DropDownList ID="drpRates" runat="server" onchange="ratesDrpFunc(); return false;">
</asp:DropDownList>
<asp:DropDownList ID="drpTimeLimit" runat="server" onchange="timleLimitDrpFunc(); return false;">
</asp:DropDownList>
<asp:Label ID="lblMablaghGhabelePardakht" runat="server" Text="0"></asp:Label>
<span>ریال</span>
<asp:Button ID="btnSubmit" runat="server" Text="ثبت" class="FormButton" OnClick="btnSubmit_Click"
ValidationGroup="submit" />
评论后的更改:我进行了以下更改,但返回空(0)...我的问题在哪里?!
function SetPrice() {
var pElem = document.getElementById('<%=lblMablaghGhabelePardakht.ClientID%>');
pElem.innerHTML = TotalPrice;
pElem.value = TotalPrice;
var hElem = document.getElementById('<%=hndTotalPrice.ClientID%>');
hElem.valu = TotalPrice;
}
</script>
<asp:HiddenField ID="hndTotalPrice" runat="server" />
.
protected void btnSubmit_Click(object sender, EventArgs e)
{
string strtest = hndTotalPrice.Value;
// Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text);
Int64 totalPrice = Convert.ToInt64(hndTotalPrice.Value);
System.Windows.Forms.MessageBox.Show(totalPrice.ToString());
return;
}
答:我用了 hElem.setAttribute("value", TotalPrice);
而不是 hElem.valu = TotalPrice;
我的问题解决了。
来自@SJuan76对隐藏字段的评论以及您关于如何使用它的问题:
<asp:HiddenField ID='someID' runat='server' />
然后在知道最终价格后使用 javascript 设置值。您需要在 javascript 中使用客户端 ID。
这是一篇关于此的文章: http://www.jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/
将您的SetPrice
函数更改为此函数,看看是否可以解决问题,这里的"TotalPrice"应该是参数。
function SetPrice(TotalPrice) {
var pElem = document.getElementById('<%= lblMablaghGhabelePardakht.ClientID %>');
pElem.innerHTML = TotalPrice;
pElem.value = TotalPrice;
var hElem = document.getElementById('<%= hndTotalPrice.ClientID %>');
hElem.valu = TotalPrice;
}
您没有在设置价格函数中声明TotalPrice
,因此它不知道那是什么。因此,它可以基于此返回 null/0。
相关文章:
- 云代码(javascript)失败,原因是:{代码:1,消息:内部错误
- 简化此If语句,使其不会't重复代码-Javascript
- 十进制到二进制代码Javascript
- 第二次单击时执行不同的代码(JavaScript)
- 在不使用jquery($symbol)knockout.js的情况下分离代码javascript
- 缩短几个元素的onclick代码 - JavaScript
- 英国银行排序代码 JavaScript 正则表达式
- 我无法在谷歌图表的代码 javascript 中获取 json 数据(项目 Python Hello Dashboard
- 和代码 JavaScript 没有被 &.
- 我怎样才能减少这段代码 javascript 代码,这样它就不会那么重复了
- 我想单击一个按钮,一次只执行已经制作的程序中的一行代码.(JavaScript)
- 滑动 DIV 代码 Javascript 的小问题
- 尝试重写代码 JavaScript
- 是否可以检测到在不使用键代码 javascript 的情况下按下了输入键
- 输入网址代码 JavaScript 播放器
- 管理通用客户端代码(javascript/css)
- 理解代码javascript
- 加载json-url参数代码(javascript修改)
- 变量检查错误的代码(javascript)
- css属性仅由代码javascript应用