【问题标题】:Form will not submit, Uncaught TypeError: Illegal invocation表单不会提交,未捕获类型错误:非法调用
【发布时间】:2017-10-06 13:26:39
【问题描述】:

我尝试使用的表单无法提交,我在尝试时不断在控制台中收到“未捕获的类型错误:非法调用”。
非常感谢任何帮助,我不确定我是否不小心使用了导致问题的系统/保留字。

    <script>
$(document).ready(function($) {
    $("#submit_btn").click(function() { 
        //get input field values
        var user_name       = $('input[name=name]').val(); 
        var user_email      = $('input[name=email]').val();
        var user_add1      = $('input[name=add1]').val();
        var user_add2      = $('input[name=add2]').val();
        var user_town      = $('input[name=town]').val();
        var user_country      = $('input[name=country]').val();
        var user_postcode      = $('input[name=postcode]').val();
        var user_tel      = $('input[name=tel]').val();
        var user_entries      = $('input[name=entries]').val();
        var user_freeentries      = $('input[name=freeentries]').val();
        var user_entries      = $('input[name=entries]').val();
        var user_total      = $('input[name=total]').val();
        var user_question      = $('select[name=question]').val();

        //simple validation at client's end
        //we simply change border color to red if empty field using .css()
        var proceed = true;
        if(user_name==""){ 
            $('input[name=name]').css('border-color','red'); 
            proceed = false;
        }

        //everything looks good! proceed...
        if(proceed) 
        {

            //data to be sent to server
            post_data = {'userName':user_name, 'userEmail':user_email, 'AddressLine1':user_add1, 'AddressLine2':user_add2, 'town':user_town, 'country':user_country, 'postcode':user_add1, 'tel':user_tel, 'entries':user_entries, 'freeentries':user_freeentries, 'total':total, 'question':user_question};

            //Ajax post data to server
            $.post('enter.php', post_data, function(response){  

                //load json data from server and output message     
                if(response.type == 'error')
                {
                    output = '<div class="error">'+response.text+'</div>';
                }else{
                    output = '<div class="success">'+response.text+'</div>';

                    //reset values in all input fields
                    $('#contact_form input').val('');   
                    $('#contact_form select').val(''); 
                }

                $("#result").hide().html(output).slideDown();
            }, 'json');

        }
    });

    //reset previously set border colors and hide all message on .keyup()
    $("#contact_form input, #contact_form select").keyup(function() { 
        $("#contact_form input, #contact_form select").css('border-color',''); 
        $("#result").slideUp();
    });

});(jQuery);
</script> 

这是供参考的表格。它正在测试中,没有奇怪的字符,只是纯文本。

<fieldset id="contact_form">
                  <div id="result"></div>
                  <input type="text" name="name" id="name" placeholder="Name" />
                  <input type="email" name="email" id="email" placeholder="Email" />
                  <input type="text" name="add1" id="add1" placeholder="Address Line 1" />
                  <input type="text" name="add2" id="add2" placeholder="Address Line 2" />
                  <input type="text" name="town" id="town" placeholder="Town" />
                  <input type="text" name="country" id="country" placeholder="Country" />
                  <input type="text" name="postcode" id="postcode" placeholder="Post Code" />
                  <input type="text" name="tel" id="tel" placeholder="Telephone number" />
                  <input onblur="findTotal()" type="text" name="entries" id="entries" placeholder="Number" />
                  <input type="text" name="freeentries" id="freeentries" placeholder="Number" />
                  <input type="text" name="total" id="total" placeholder="Cost of Entry" />

                  <select style="color:#666" name="question">
                  <option value="">Please Choose...</option>           
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                  </select>
                    <br><Br><br><br><br>


                  <label><span>&nbsp;</span>
                    <button class="submit_btn animate swing" data-delay="500" id="submit_btn">Enter</button>
                  </label>
                </fieldset>

                    <script type="text/javascript">
function findTotal(){
    var arr = document.getElementsByName('entries');
    var tot=0;
    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('total').value = "£" + tot * 50;
}

    </script>

【问题讨论】:

  • 完整的错误是什么?
  • 注意你有一个';'在脚本的末尾,因此 jQuery 不会传递给变量 $
  • Uncaught TypeError: Illegal invocation at e (js/jquery.js:4:24391) at Wc (js/jquery.js:4:24276) at Wc (/js/jquery.js:4 :24299) 在 Wc (/js/jquery.js:4:24299) 在 Function.n.param (/js/jquery.js:4:24637) 在 Function.ajax (/js/jquery.js:4:20596) ) 在 Function.n.(匿名函数) [as post] (/js/jquery.js:4:22698) 在 HTMLButtonElement. (life/index1.html:975:15) 在 HTMLButtonElement.dispatch (/js /jquery.js:3:8066) 在 HTMLButtonElement.r.handle (/js/jquery.js:3:4767)
  • 哪个';'你也是指的吗,能具体点吗,谢谢
  • });(jQuery); 应该是 })(jQuery);。它当前将 $ 覆盖为 null,因为您没有传递值。其实你甚至不需要这样做,只需要使用$(function() { //yourcode.. },见:learn.jquery.com/using-jquery-core/document-ready

标签: javascript jquery html forms


【解决方案1】:

function findTotal(){
    var arr = document.getElementsByName('entries');
    var tot=0;
    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('total').value = "£" + tot * 50;
}

$(document).ready(function() {
    $("#submit_btn").click(function() {
        //get input field values
        var user_name       = $('input[name=name]').val(); 
        var user_email      = $('input[name=email]').val();
        var user_add1      = $('input[name=add1]').val();
        var user_add2      = $('input[name=add2]').val();
        var user_town      = $('input[name=town]').val();
        var user_country      = $('input[name=country]').val();
        var user_postcode      = $('input[name=postcode]').val();
        var user_tel      = $('input[name=tel]').val();
        var user_freeentries      = $('input[name=freeentries]').val();
        var user_entries      = $('input[name=entries]').val();
        var user_total      = $('input#total').val();
        var user_question      = $('select[name=question]').val();
        console.log(user_total)
        //simple validation at client's end
        //we simply change border color to red if empty field using .css()
        var proceed = true;
        if(user_name==""){ 
            $('input[name=name]').css('border-color','red'); 
            proceed = false;
        }

        //everything looks good! proceed...
        if(proceed) 
        {

            //data to be sent to server
            post_data = {'userName':user_name, 'userEmail':user_email, 'AddressLine1':user_add1, 'AddressLine2':user_add2, 'town':user_town, 'country':user_country, 'postcode':user_add1, 'tel':user_tel, 'entries':user_entries, 'freeentries':user_freeentries, 'total':user_total, 'question':user_question};
            console.log(post_data)

            //Ajax post data to server
            $.post('enter.php', post_data, function(response){  

                //load json data from server and output message     
                if(response.type == 'error')
                {
                    output = '<div class="error">'+response.text+'</div>';
                }else{
                    output = '<div class="success">'+response.text+'</div>';

                    //reset values in all input fields
                    $('#contact_form input').val('');   
                    $('#contact_form select').val(''); 
                }

                $("#result").hide().html(output).slideDown();
            }, 'json');

        }
    });

    //reset previously set border colors and hide all message on .keyup()
    $("#contact_form input, #contact_form select").keyup(function() { 
        $("#contact_form input, #contact_form select").css('border-color',''); 
        $("#result").slideUp();
    });

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Here is the form for reference. it is being tested with no strange characters just plain text.

<fieldset id="contact_form">
                  <div id="result"></div>
                  <input type="text" name="name" id="name" placeholder="Name" />
                  <input type="email" name="email" id="email" placeholder="Email" />
                  <input type="text" name="add1" id="add1" placeholder="Address Line 1" />
                  <input type="text" name="add2" id="add2" placeholder="Address Line 2" />
                  <input type="text" name="town" id="town" placeholder="Town" />
                  <input type="text" name="country" id="country" placeholder="Country" />
                  <input type="text" name="postcode" id="postcode" placeholder="Post Code" />
                  <input type="text" name="tel" id="tel" placeholder="Telephone number" />
                  <input onblur="findTotal()" type="text" name="entries" id="entries" placeholder="Number" />
                  <input type="text" name="freeentries" id="freeentries" placeholder="Number" />
                  <input type="text" name="total" id="total" placeholder="Cost of Entry" />

                  <select style="color:#666" name="question">
                  <option value="">Please Choose...</option>           
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                  </select>
                    <br><Br><br><br><br>


                  <label><span>&nbsp;</span>
                    <button class="submit_btn animate swing" data-delay="500" id="submit_btn">Enter</button>
                  </label>
                </fieldset>

$.post 请求中的post_data 序列化存在问题。检查post_data 对象中的所有值是否正确序列化:这意味着您的数据必须是普通对象或字符串或数组。您也可以尝试在ajax设置中设置processData: false(可以找到列表here)。

编辑:再次查看 html 和您的代码后,结果表明您传递了错误的值,而不是输入“total”的值,total 而不是正确的 user_total,导致错误。 正确的`post_data。请参阅更正后的 sn-p。

【讨论】:

  • 我从哪里开始序列化帖子数据...抱歉,我以前从未如此深入地传递表单数据,请温柔...
  • 我已经编辑了我的答案。也许也发布您的 html 表单,以便我们可以在 sn-p 中尝试?
  • 我已更新问题以包含表单数据,谢谢
  • 我已经更新了我的答案,请现在看看是否可行
猜你喜欢
  • 1970-01-01
  • 2019-04-29
  • 2017-06-16
  • 2014-01-03
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多