2013-02-26 35 views
0

當用戶單擊該按鈕時,我需要將以下TextField的值提交給位於/TestService/SaveMethod的Webservice。如何獲取文本字段值並通過webservice發送該值

<div class="content"> 
        @Html.TextBoxFor(mo => mo.id, new { id = "id" })  
       <input type="text" id="fname" name="fname" />  
       <input type="submit" value="send" class="save"/> 

     </div> 

的jQuery:

$(function() { 
    $('.save').click(function() { // How to grab the values of those textfields and send it over the webservice located at `/TestService/SaveMethod` }); 


}); 

回答

0

您不需要jQuery的除非您想使用它,太阿賈克斯。只要把一種形式在HTML這樣的:

<div class="content"> 
    @using(Html.BeginForm("SaveMethod","TestService",new{},FormMethod.Post,new{}){ 
    @Html.TextBoxFor(mo => mo.id, new { id = "id" },new{})  
    <input type="text" id="fname" name="fname" />  
    <input type="submit" value="send" class="save"/> 
    } 
    </div> 
+0

好吧,那麼如果操作成功或失敗,我將如何獲得服務器響應? – 2013-02-26 09:25:51

+0

您可以根據您得到的迴應顯示回覆,我的意思是標題信息。但是,留下那個,然後去ajax。 – gaurav 2013-02-26 09:29:43

0

可以在腳本中獲取文本字段值使用VAL訪問其值()方法,並送價值用ajax帖子的WebService。

$(function() { 
    $('.save').click(function() { 

var name = $("#taYourName").val(); 
     $.ajax(
     { 
      Type: "POST", 
      contentType: "application/xml", 
      url: "YourNameIs.asmx/YourName", 
      data: { yourName: name }, 
      success: function (msg) { 
       $("#lblForAjax").text(msg); 
      } 
     }); 


}); 
}); 
0

應該是這樣的:它會給你名字的價值。

$(function() { 
    $('.save').click(function() 
    { 
     var name = $("#fname").val(); 
    }); 
}); 
0

你可以用jquery ajax來做到這一點。這是一個經過測試的代碼。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title>Test webservices</title> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

    <script> 
     $(document).ready(function() { 
      $('#submit').click(function (e) { 
       e.preventDefault(); 
       var x = $('#myval').val(); 
       $.ajax({          
        url: 'webservice.php', 
        type: 'GET', 
        contentType: 'text/html', 
        dataType: 'text', 
        data: { 
         data : x 
        }, 
        success: function(response) 
        { 
         alert("data send to webservice"); 
        }, 
        error: function (response) 
        { 
        }, 
       });   
      }); 
     }); 

    </script> 
</head> 
<body> 
    <form action='' method='get'> 
     <input type='text' name='myval' id='myval' > 
     <input type='submit' value='Go' id='submit' > 
    </form> 

</body> 
</html> 

我希望這能解決您的問題。

相關問題