[英]How to send a value from a form to a Javascript function in cshtml?
我目前正在嘗試通過cshtml文件中的表單調用javascript函數。 表單中會有一個文本框,用於接收來自用戶的字符串和“發送”按鈕,這些字符串會將字符串發送到javascript函數。
形式的腳本:
@using (Html.BeginForm("SearchStreet", "CustomerInformation", FormMethod.Get, new { @class = "form-inline" }))
{
<input type="hidden" name="CustId" value="@ViewBag.CustId" />
<input type="hidden" name="AddressId" value="@ViewBag.AddressId" />
<div class="form-group">
<label for="street">Street Name</label>
@Html.TextBox("street", null, htmlAttributes: new { @class = "form-control" })
</div>
<div class="form-group">
<label></label>
<input type="submit" value="Send" class="form-control btn btn-default" />
</div>
}
如何將字符串發送到特定的Javascript函數? 謝謝。
@using (Html.BeginForm("SearchStreet", "CustomerInformation", FormMethod.Get, new { @class = "form-inline" }))
{
<input type="hidden" name="CustId" value="@ViewBag.CustId" />
<input type="hidden" name="AddressId" value="@ViewBag.AddressId" />
<div class="form-group">
<label for="street">Street Name</label>
@Html.TextBox("street", null, htmlAttributes: new { @class = "form-control" })
</div>
<div class="form-group">
<label></label>
<input type="submit" onclick="sendValue();" value="Send" class="form-control btn btn-default" />
</div>
}
<script type="text/javascript">
function sendValue(){
var element = document.getElementById(street);
//Get value by element.value, rest you know what to do
}
添加onclick發送按鈕。
<div class="form-group">
<label></label>
<input type="button" onclick="checkText();" value="Send" class="form-control btn btn-default" />
</div>
在JavaScript中
<script type="text/javascript">
function checkText(){
var number=document.getElementById("street").value;
//DO you stuff here
}
</script>
當按鈕將被單擊時,您需要將文本框值發送到javascript函數,所以我有以下方案嘗試。 它可能會幫助您。
-腳本部分-
<script type="text/javascript">
function DemoFunction()
{
var stringValue1=$('#street').val(); --> if you use jQuery
var stringValue2=document.getElementById("street").value; --> if you use
only javascript
alert(stringValue1);
alert(stringValue2);
}
</script>
-查看部分-
-- add 'onclick="return DemoFunction()' this to your button
ex:-
<input type="submit" value="Send" class="form-control btn btn-default"
onclick="return DemoFunction()" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.