2015-07-13 169 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它。

相关问题