2017-04-05 4 views
0

私は現在、cshtmlファイル内のフォームでjavascript関数を呼び出そうとしています。フォームには、ユーザーからの文字列を受け取るテキストボックスと、文字列をjavascript関数に送信する「送信」ボタンがあります。フォームのcshtmlのフォームから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関数に文字列を送信する方法は?おかげさまで

+0

は「(その方法でgetElementByIdをを使用して、そのテキストボックスの値を取得し、ボタンにIDを "onclickの" メソッドを追加ここに行く ") – Rex

答えて

1

送信ボタンに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> 
2
@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 
} 

+0

ありがとう!私はそれを試してみましょう:D – Farid

0

JavaScriptで。それはあなたを助けるかもしれません。

- スクリプトSection--

<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()" />