2013-10-01 57 views
4

我有一個沒有下拉列表如下如何用它的名字將焦點設置下拉-javascript/jQuery的

<select name="dropname[0]">....</select> 
<select name="dropname[1]">....</select> 
<select name="dropname[2]">....</select> 
<select name="dropname[3]">....</select>.... 

現在我想將焦點設置第二個下拉list.I嘗試這些

document.getElementsByName('dropname')[1].focus(); 

結果此錯誤提前

TypeError: document.getElementsByName(...)[1] is undefined 

感謝

回答

5

既然你用jQuery標記你的問題,你可以試試

$('select[name^="dropname"]').eq(1).focus(); 

// $('select[name^="dropname"]') < elements whos name starts with "dropname" 
// .eq(1) < select one based on its index 
// .focus(); < use jQuery's focus method to set the focus 
+1

感謝Johan ....其作品 – manuthalasseril

+0

@manuthalasseril沒問題!請注意名稱實際上是'dropname [0]','dropname [1]'等等。也許一個普通的類可能更適合這種情況。 – Johan

+0

其實這些下拉列表是在server.so中的一個數組下處理的。 – manuthalasseril

6

嘗試使用jQuery專注如一

$("#id2").focus(); 
1

您可以使用

var i = 0 //or 1,2,3 

document.getElementsByName('dropname['+i+']')[0].focus(); 

希望這將幫助你

相關問題