【问题标题】:Javascript submit form not getting submitted in Firefox and IEJavascript 提交表单未在 Firefox 和 IE 中提交
【发布时间】: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


【解决方案1】:

将表单附加到正文

document.getElementsByTagName('body')[0].appendChild(submitForm);

【讨论】:

  • document.body.append(...) 也会这样做; )。
猜你喜欢
  • 2012-07-01
  • 1970-01-01
  • 2013-03-09
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
相关资源
最近更新 更多