Javascript 未捕获类型错误:无法读取属性';重置';未定义的
我正在尝试将窗体值重置为初始值: 这是正在使用的jquery,这是获取错误的行。特别是$(“#gquestion”)[0].reset() 它的名字是这样的,因为我希望它发生在窗体的隐藏处:Javascript 未捕获类型错误:无法读取属性';重置';未定义的,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试将窗体值重置为初始值: 这是正在使用的jquery,这是获取错误的行。特别是$(“#gquestion”)[0].reset() 它的名字是这样的,因为我希望它发生在窗体的隐藏处: $("#gquestion").hide(questionhide()); 在此文件中: $(document).ready(function() { $("#passwordreset").hide(passwordhide()); $("#hardwareissue").hide(hardwa
$("#gquestion").hide(questionhide());
在此文件中:
$(document).ready(function() {
$("#passwordreset").hide(passwordhide());
$("#hardwareissue").hide(hardwarehide());
$("#softwareissue").hide(softwarehide());
$("#servicerequest").hide();
$("#question").hide(questionhide());
$("#problemtype").change(function() {
if ($("#problemtype").val() == "passwordreset") {
$("#question").hide(questionhide());
$("#hardwareissue").hide(hardwarehide());
$("#softwareissue").hide(softwarehide());
$("#servicerequest").hide();
$("#passwordreset").show();
} else if ($("#problemtype").val() == "hardware") {
$("#question").hide(questionhide());
$("#passwordreset").hide(passwordhide());
$("#softwareissue").hide(softwarehide());
$("#servicerequest").hide();
$("#hardwareissue").show();
} else if ($("#problemtype").val() == "software") {
$("#passwordreset").hide(passwordhide());
$("#question").hide(questionhide());
$("#hardwareissue").hide(hardwarehide());
$("#softwareissue").show();
} else if ($("#problemtype").val() == "servicerequest") {
$("#servicerequest").show();
} else if ($("#problemtype").val() == "question") {
$("#passwordreset").hide(passwordhide());
$("#hardwareissue").hide(hardwarehide());
$("#softwareissue").hide(softwarehide());
$("#question").show();
}
});
// Password jquery handling ---------------------------------
function passwordhide() {
$("#system option:eq(0)").attr('selected','selected');
$("#passwordreset")[0].reset();
$("#otherdiv").hide();
}
$("#system").change(function() {
if ($("#system").val() == "Other") {
$("#otherdiv").show();
}
else {
$("#otherdiv").hide(function () {
$("#pwother").val('');
});
}
});
// General Question handling ---------------------------------
function questionhide() {
$("#question")[0].trigger('reset');
}
这是它所指的整个html表单:
<form method="POST" action="/ticket" id="gquestion">
{{ csrf_field() }}
<input name="probtype" type="hidden" value="General Question">
<div class="form-group">
<label form="control-label">Please tell us your question:</label>
<textarea class="form-control" name="other" id="gqtext"></textarea>
<br>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
工作,但我想知道为什么它不会与重置行,我现在有其他形式的工作。标题是加载时出现的错误。请尝试以下操作之一:
$("#gquestion").hide(100, questionhide()); // You can change 100 to any other number. It represents the duration of the hide effect.
或:
和更改:$(“#gquestion”)[0].reset()代码>到$(“#gquestion”)。触发器('reset')代码>
没有测试它,但它应该可以工作
您也可以尝试:
function questionhide(){
document.forms.namedItem("gquestion").reset();
}
在我看来,您的函数是在加载文档之前创建的,使得$('#gquestion')
未定义。您没有正确调用hide方法。在这里检查jquery文档。抱歉,但是我看不出我是如何错误地调用hide方法的。我还有其他一些表单,它们以完全相同的方式调用它(实际上只是更改id引用和被调用的函数),它们按照我的要求隐藏。如果我只想隐藏表单,那么它就是$(“#gquestion”).hide();Hide有3个重载。一个没有任何参数的,您显然没有调用它。这就给我们留下了A)隐藏([duration][,complete]),B)隐藏(options)和C)隐藏(duration[,easing][,complete])。现在让我们看看您的问题类型hide()。这是一个函数,因此,您没有调用重载a和C。这就给我们留下了重载B。因为函数是Javascript中的一个对象,所以它试图将您的questionhide解释为hide()的选项。但这显然不是你的意图。这就是我说你没有正确调用它的意思。所以我将它改为$(“#gquestion”).hide({complete:questionhide()}),我的重置问题仍然存在。相同的错误消息?如果是,请张贴完整的代码。你把表格合上了,对吗?我试过第二个了。但是我的错误仍然存在于$(“#gquestion”)[0].reset()行的函数中。是否按照第二步,调用触发器而不是reset方法?刚刚这样做,它就在该行的原因上更改为“Uncaught TypeError:无法读取未定义的属性'trigger'
function questionhide() {
$("#gtext").val('');
}
$("#gquestion").hide(100, questionhide()); // You can change 100 to any other number. It represents the duration of the hide effect.
$("#gquestion").hide({complete: questionhide()});
function questionhide(){
document.forms.namedItem("gquestion").reset();
}