簡體   English   中英

從JQuery函數獲取數組到后面的C​​#代碼

[英]Get array from JQuery function to C# code behind

我一直試圖將jquery函數的輸出傳遞給我的c#頁面代碼,以進行一些處理,但我想不出如何正確完成它,但我知道這是有可能的。

我的HTML代碼如下:

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>jQuery Get Selected Radio Button Value</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("button").click(function () { var items = []; $.each($("input[name]:checked"), function () { items.push($(this).val()); }); $.ajax({ url: 'WebForm1.aspx/LoadStrings', method: 'post', contentType: 'application/json', data: '{jsonString:' + items + '}', dataType: 'json', }); alert("You entered: " + items.join(", ")); }); }); </script> </head> <body> <h4>Please select your gender.</h4> <p> <label> <input type="radio" name="gender" value="male" />Male</label> <label> <input type="radio" name="gender" value="female" />Female</label> <br /> <br /> <label> <input type="radio" name="address" value="Kingston" />Kingston</label> <label> <input type="radio" name="address" value="Saint Catherine" />Saint Catherine</label> </p> <button type="button">Get Values</button> </body> </html> 

請幫助我將jquery函數中的“ items”變量傳遞給我的代碼,

 [WebMethod] public static string[] LoadStrings(string[] jsonString) { } 

由於字符串連接, '{jsonString:' + items + '}'存在錯誤,您將獲得一個字符串{jsonString:Hello World,How are you} ,但是JSON有效字符串必須為{"jsonString": "Hello World", "How are you"}

請使用JSON.stringify創建JSON有效字符串JSON.stringify({jsonString: items})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM