0
我試圖在任何文本之後關注文本區域。這適用於IE和Chrome,但是在Firefox中它在文本之前開始。jQuery TypeError undefined with setSelectionRange
什麼是以下錯誤的原因:
TypeError: $(...)[0] is undefined
[Break On This Error]
$(focus)[0].setSelectionRange($(focus).val().length, $(focus).val().length);
相關代碼:
$(focus)[0].setSelectionRange($(focus).val().length, $(focus).val().length);
$(focus).focus();
全jQuery代碼:
<script src="//cdn.jsdelivr.net/jquery.cookie/1.3/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#state").change(function() {
this.form.submit();
})
var focus = $.cookie("inputFocus");
$(focus)[0].setSelectionRange($(focus).val().length, $(focus).val().length);
$(focus).focus();
$("#supplier_name").val($("#supplier_name").val());
$("#aircraft_type").val($("#aircraft_type").val());
var typingTimer;
var doneTypingInterval = 600;
$('#supplier_name').keyup(function(){
clearTimeout(typingTimer);
if ($('#supplier_name').val) {
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
$.cookie("inputFocus", "#supplier_name");
});
$('#aircraft_type').keyup(function(){
clearTimeout(typingTimer);
if ($('#aircraft_type').val) {
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
$.cookie("inputFocus", "#aircraft_type"); });
function GetQueryStringParams(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
var state = GetQueryStringParams('state');
var supplier_name = GetQueryStringParams('supplier_name');
var aircraft_type = GetQueryStringParams('aircraft_type');
if(supplier_name === "" && state === "any" && aircraft_type === "") {
$('#clear').attr('disabled','disabled');
}
$("#clear").click(function() {
if(state === "any") {
$("#aircraft_type").val("");
$("#supplier_name").val("");
} else {
$('#state option:selected').remove();
$("#aircraft_type").val("");
$("#supplier_name").val("");
}
});
function doneTyping() {
$("form").submit();
}
});
</script>
該Cookie可能未設置捕獲它尚未 – Alexander
shoudn't它是如果(... val())與palenthesis。除此之外,試試$(focus)[0] .selectionStart = $(focus)[0] .selectionEnd = $(focus).val()。length; –
你是什麼意思應該是「if(... val())」?我曾嘗試你的代碼,它吐出以下錯誤: 類型錯誤:$(...)VAL(...)是未定義 [打破這個錯誤] \t $(焦點)[0 ] .selectionStart = $(focus)[0] .selectionEnd = $(focus).val()。length; – sephiith