2009-12-22 30 views
24
<script> 
function urlencode(str) { 
    return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40'); 
} 
$('input#q').keyup(function(e) { 
if(e.keyCode == 13) { 
    if($('input#q').val().length > 2) 
    { 
    $('input#q').val() = urlencode($('input#q').val()); 
    document.search_form.submit(); 
    } 
} 
}); 
$('input#search').click(function() { 
if($('input#q').val().length > 2) 
{ 
    $('input#q').val() = urlencode($('input#q').val()); 
    document.search_form.submit(); 
} 
}); 
</script> 

khi tôi nhấp vào nút tìm kiếm tôi nhận được lỗi sau: "ReferenceError chưa gặp: phía bên tay trái không hợp lệ trong chuyển nhượng" Nhưng code đang thực sự giống như khi tôi nhấn "enter". Ai đó có thể giải thích?ReferenceError chưa gặp: không hợp lệ phía bên tay trái trong phân

+0

Trong hàm 'urlencode()', bạn nên sử dụng 'encodeURI()' hoặc 'encodeURIComponent()' thay vì 'escape()', không được dùng khoảng mười năm trước: http://msdn.microsoft. com/vi-us/library/xh9be5xc (VS.85) .aspx http://msdn.microsoft.com/en-us/library/aeh9cef7(VS.85).aspx https://developer.mozilla.org/ vi/Core_JavaScript_1.5_Reference/Global_Functions/encodeURI https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/encodeURIComponent – NickFitz

Trả lời

33

Bạn không thể gán một giá trị mới cho kết quả của một hàm

$('input#q').val() = urlencode($('input#q').val()); 

Sử dụng này để thay thế:

$('input#q').val(urlencode($('input#q').val())) 

Nó sẽ không làm việc với các phím nhấn một trong hai - có thể trang được chỉ đơn giản là gửi sau khi cùng một lỗi js xảy ra.

Các vấn đề liên quan