【发布时间】: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> </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