2017-05-27 39 views
0

我想在document.ready(function)中获取多个文本框ID。如何在jQuery中获取多个文本框ID

我试图得到它,但它不工作。

如何使用逗号分隔符或其他方式..?

$(document).ready(function($){ 
    $(".Table-right .MinValue, .Carat-right .MinValue,.Price-right .MinValue,.Depth-right .MinValue ").attr('id'); 
}) 

回答

1

$(document).ready(function($) { 
 

 
    var output = $(".Table-right .MinValue, .Carat-right .MinValue,.Price-right .MinValue,.Depth-right .MinValue "); 
 

 
    
 
    output.each(function(index) { 
 

 
    console.log(index + ": " + $(this).attr('id')); 
 
    }); 
 

 

 
});
Not Sure if this is what you are going for but if this is how you set up your data than you did it correctly. 
 

 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 
<div class="Table-right"> 
 
    <input type="text" class="MinValue" id="test1"> 
 

 
</div> 
 

 
<div class="Carat-right"> 
 
    <input type="text" class="MinValue" id="test2"> 
 

 
</div> 
 

 
<div class="Price-right"> 
 
    <input type="text" class="MinValue" id="test3"> 
 

 
</div> 
 

 
<div class="Depth-right"> 
 
    <input type="text" class="MinValue" id="test4"> 
 

 
</div>

1

你的选择给出了一个nodelist。所以你必须遍历他们取得个人id像这样:

$(document).ready(function($){ 
    $(".Table-right .MinValue, .Carat-right .MinValue,.Price-right .MinValue,.Depth-right .MinValue ").each(function(){ 
    alert($(this).attr(id)) 
}); 
}) 
相关问题