2015-07-13 168 views
1

我對jQuery和圖表非常陌生。這是我的腳本,它工作正常。它給了我用戶選擇的複選框的ID。我在我的控制器中有一個圖表操作,它也可以正常工作,但它會使用我所有的值創建圖表。我希望它根據腳本中選定的值創建圖表。我不知道如何將選定的值傳遞給我的控制器。從JavaScript MVC控制器傳遞數據

var checkboxes = $("input[type='checkbox']"); 
$(function() 
{ 
    function getValueUsingClass() { 
     /* declare an checkbox array */ 
     var chkArray = []; 

     /* look for all checkboes that have a class 'chk' attached to it and check if it was checked */ 
     $(".chk:checked").each(function() { 
      chkArray.push($(this).val()); 
     }); 

     /* we join the array separated by the comma */ 
     var selected = chkArray.join(",") + ","; 

     /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */ 
     if (selected.length > 1) 
     { 
      alert("You have selected " + selected); 
     } 
     else 
     { 
      alert("Please check at least one of the checkbox"); 
     }   
    } 

    $("#Charter").click(function() { 
     getValueUsingClass(); 
    }); 
}); 
+0

您的操作方法未採用任何參數。你想發送什麼數據? – Shyju

+0

我試圖發送arrar chkArray中的值到我的控制器中的我的Charter Action – lagfvu

+0

迭代等Instaed,更簡單的是通過提取選中的複選框$('input [type =「checkbox」]:checked')。爲了溝通,您可以使用ajax並在您的控制器中返回JSON。然後在highcharts創建使用$ .getJSON()和加載數據/選項 –

回答

0

回報,你想在你的js函數填充使用return selected;然後通過發佈形式或使用AJAX將其發回的可變後的數據。

綁定您的數據元素的查看頁面上,例如:

<input name="foo" id="yourId" value="bar" /> 

然後修改它的值:

$('#foo').val(getValueUsingClass()); 

,並通過發佈表單到控制器通過模型回來。

如果你想發送數據到你的控制器異步,那麼你可以看看Ajax

0

您可以使用ajax在getValueUsingClass()內調用您的控制器方法。

它可能會是這個樣子:

$.ajax({ 
    url: "/YourControllerName/Chart", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    data: { arr: chkArray }, 
    success: function() { 
     // do things upon success 
    }, 
    error: function() { 
     alert("Error!"); 
    } 
}); 

也就是說,爲您的控制器動作有一個名爲arr參數,因爲JSON一旦它被傳遞給控制器​​映射chkArray它。

相關問題