【发布时间】:2014-04-15 05:55:37
【问题描述】:
我正在将 javascript 中的表单提交给 C# MVC 中的控制器,它在 chrome 中很容易提交,但在 Firefox 和 IE 中却没有
//CSHTML CODE
<th class="gen2">
<button type="button" id="buttonClass">Generate</button>
</th>
<td class="money"><input type="checkbox" class="chk" name="checkboxID" value=@item.WithdrawalID></td>
//Javascript code
$("#buttonClass").click(function () {
getValueUsingClass();
});
function getValueUsingClass() {
var data = "";
var submitForm = document.createElement('form');
//Creating a form and giving the attributes
submitForm.name = "formSubmit";
submitForm.id = "formSubmit";
submitForm.method = "post";
submitForm.action = "generatebankfile";
var chkArray = \[\];
alert(chkArray);
$(".chk:checked").each(function () {
chkArray.push($(this).val());
});
for (var i = 0; i < chkArray.length; i++) {
data = data + chkArray\[i\];
if (i != chkArray.length - 1) {
data = data + ',';
}
}
var element = document.createElement("input");
element.name = "checkboxID";
element.value = data;
submitForm.appendChild(element);
if (chkArray.length > 0) {
submitForm.submit();
}
else {
alert("Please select at least one of the checkbox");
}
}
【问题讨论】:
-
是否需要创建
form并提交?您可以使用 ajax 将数据发布到任何Action方法。您以任何方式构建需要发布的内容。只需对它们进行字符串化并将其发送到操作。我知道这不是您问题的答案,只是认为这会是更好的方法。 -
看起来您没有将新创建的表单附加到页面。也尝试附加表单。
-
Firefox 控制台中的任何错误.. ?
标签: c# javascript jquery asp.net-mvc forms