我对 jQuery 和图表真的很陌生。这是我的脚本,效果很好。它为我提供了用户选择的复选框的 ID。我的 Controller 中有一个图表操作,它也可以正常工作,但它使用我的所有值创建一个图表。我希望它根据我的脚本中选定的值创建一个图表。我不知道如何将选定的值传递到我的 Controller 。
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();
});
});
最佳答案
使用 return selected;
填充变量后,在 js 函数中返回所需的数据,然后通过发布表单或使用 ajax 将其发回。
将数据绑定(bind)到“ View ”页面上的元素,例如:
<input name="foo" id="yourId" value="bar" />
然后修改它的值:
$('#foo').val(getValueUsingClass());
并通过将表单发布到 Controller 来传回模型。
如果您希望将数据异步发送到 Controller ,那么您可以查看 Ajax .
关于javascript - 从 Javascript MVC Controller 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384443/