2012-02-16 32 views
6

Tôi đang cố gắng viết một JQuery để tập trung vào hộp văn bản hiển thị đầu tiên, được bật hoặc văn bản ở dạng thứ hai khi trang tải. Trong ví dụ bên dưới, hãy tập trung vào đầu vào văn bản2 trong form2.JQuery tập trung vào hộp văn bản đầu tiên hoặc văn bản khi trang tải

Giải pháp được đề xuất trong jQuery Dynamically focus on the first INPUT or Textarea không hoạt động đối với tôi.

Xin lưu ý rằng chúng tôi có nhiều trang trong ứng dụng của chúng tôi. Trong một số trang, trường đầu tiên của biểu mẫu là một hộp văn bản và trong các trang khác, trường đầu tiên là một vùng văn bản. Tôi muốn viết một phương thức JQuery sẽ thực hiện công việc cho tất cả các trang bất kể trường đầu tiên là một hộp văn bản hay một vùng văn bản. Trong ví dụ bên dưới, giải pháp sẽ hoạt động nếu chúng tôi hoán đổi các trường textarea1text2 xung quanh.

<div id="header"> 
    <form name="form1"> 
    <input type="text" name="text1"> 
    </form> 
</div> 
<div id="content"> 
    <form name="form2"> 
    <input type="checkbox" name="chc" value="test"><br> 
    <input type="hidden" name="hide"> 
    <input type="text" name="text2"><br> 
    <textarea name="textarea1"></textarea><br> 
    <input type="text" name="text3"> 
    </form> 
</div> 

Đây là những gì tôi đã cố gắng cho đến nay ...

// throws the error - $("#content input:text, #content textarea").first is not a function 
$("#content input:text, #content textarea").first().focus(); 

// focuses on the textarea even if a text box is the first element in the form 
$("#content textarea, #content[type='text']:visible:enabled:first").focus(); 

// focuses on the last text box in the page! 
$("#content :input[type='text'], :textarea:visible:enabled:first").focus(); 

// focuses on the first text box even if a textarea is the first element in the form 
$("#content :input[type='text']:visible:enabled:first, :textarea:visible:enabled:first").focus(); 

// focuses on the checkbox as it is the first input element 
$('#content :input:visible:enabled:first').focus(); 

Cảm ơn bạn.

Trả lời

8
$("#content input:text, #content textarea").eq(0).focus() 
+0

Cảm ơn Adam! Điều này làm việc. – neo108

+0

Bất cứ lúc nào @ neo108:] –

0

Bạn nên sử dụng các phần tử DOM thay vì đối tượng jQuery, như vậy:

$("#content input:text")[0].focus() 
3

Ở đây những gì bạn cần:

​$(function(){ 
    $('form[name=form2]').find(':text, textarea').filter(":visible:enabled").first().focus(); 
})​ 

Một ví dụ here

+0

Xin chào Rafael, cảm ơn, công trình này nhưng tôi không thể tham khảo tên biểu mẫu vì tên biểu mẫu khác nhau ở mỗi trang. Id div "nội dung" giống nhau trong tất cả các trang. Có thể sử dụng id div thay vì tên biểu mẫu không? – neo108

+0

Tuyệt đối, @ neo108 ... bộ chọn cho vùng chứa có thể là bất kỳ ai loại bỏ biểu mẫu đầu tiên, vì vậy $ ('# content') có thể được sử dụng:] –

0

// ném lỗi - $ ("# content input: text, #content textarea "). Đầu tiên không phải là hàm $ (" # content input: text, #content textarea "). Đầu tiên(). Focus();

Điều này sẽ hoạt động tốt. Kiểm tra phiên bản JQuery của bạn, first method gia tăng đối với JQuery 1,4

1
<script type="text/javascript"> 
$(document).ready(function() { 
    // focus on the first visible and enabled input field or textarea 
    $(":input:visible:enabled").each(function() { 
     if (($(this).attr('type') == 'text') && ($(this).is('input'))){ 
      $(this).focus(); 
      return false; 
     } 
     if ($(this).is('textarea')){ 
      $(this).focus(); 
      return false; 
     }  
    }); 
}); 
</script> 
0

Nếu bạn sử dụng giải pháp Raphael Verger, nó có thể cũng là một mật khẩu, do:

​$(function(){ 
    $('form[name=form2]').find(':text, textarea, :password').filter(":visible:enabled").first().focus(); 
})​ 
Các vấn đề liên quan