我正在使用
$('#list option').each(function(){
//do stuff
});
循环遍历列表中的选项。 我想知道如何获得当前循环的索引?
因为我不想让var i = 0; 并且在循环内部有i ++;
答案 0 :(得分:162)
$('#list option').each(function(index){
//do stuff
console.log(index);
});
记录索引:)
下面是一个更详细的例子。
function run_each() {
var $results = $(".results");
$results.empty();
$results.append("==================== START 1st each ====================");
console.log("==================== START 1st each ====================");
$('#my_select option').each(function(index, value) {
$results.append("<br>");
// log the index
$results.append("index: " + index);
$results.append("<br>");
console.log("index: " + index);
// logs the element
// $results.append(value); this would actually remove the element
$results.append("<br>");
console.log(value);
// logs element property
$results.append(value.innerHTML);
$results.append("<br>");
console.log(value.innerHTML);
// logs element property
$results.append(this.text);
$results.append("<br>");
console.log(this.text);
// jquery
$results.append($(this).text());
$results.append("<br>");
console.log($(this).text());
// BEGIN just to see what would happen if nesting an .each within an .each
$('p').each(function(index) {
$results.append("==================== nested each");
$results.append("<br>");
$results.append("nested each index: " + index);
$results.append("<br>");
console.log(index);
});
// END just to see what would happen if nesting an .each within an .each
});
$results.append("<br>");
$results.append("==================== START 2nd each ====================");
console.log("");
console.log("==================== START 2nd each ====================");
$('ul li').each(function(index, value) {
$results.append("<br>");
// log the index
$results.append("index: " + index);
$results.append("<br>");
console.log(index);
// logs the element
// $results.append(value); this would actually remove the element
$results.append("<br>");
console.log(value);
// logs element property
$results.append(value.innerHTML);
$results.append("<br>");
console.log(value.innerHTML);
// logs element property
$results.append(this.innerHTML);
$results.append("<br>");
console.log(this.innerHTML);
// jquery
$results.append($(this).text());
$results.append("<br>");
console.log($(this).text());
});
}
$(document).on("click", ".clicker", function() {
run_each();
});
.results {
background: #000;
height: 150px;
overflow: auto;
color: lime;
font-family: arial;
padding: 20px;
}
.container {
display: flex;
}
.one,
.two,
.three {
width: 33.3%;
}
.one {
background: yellow;
text-align: center;
}
.two {
background: pink;
}
.three {
background: darkgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="one">
<select id="my_select">
<option>apple</option>
<option>orange</option>
<option>pear</option>
</select>
</div>
<div class="two">
<ul id="my_list">
<li>canada</li>
<li>america</li>
<li>france</li>
</ul>
</div>
<div class="three">
<p>do</p>
<p>re</p>
<p>me</p>
</div>
</div>
<button class="clicker">run_each()</button>
<div class="results">
</div>
答案 1 :(得分:26)
jQuery为您解决此问题。 .each()
回调函数的第一个参数是循环当前迭代的索引。第二个是当前匹配的DOM元素So:
$('#list option').each(function(index, element){
alert("Iteration: " + index)
});
答案 2 :(得分:11)
来自jQuery.each() documentation:
.each( function(index, Element) )
function(index, Element)A function to execute for each matched element.
所以你想要使用:
$('#list option').each(function(i,e){
//do stuff
});
...其中index将是索引,而element将是list
中的option元素答案 3 :(得分:3)
$('#list option').each(function(intIndex){
//do stuff
});
答案 4 :(得分:2)
惊讶地发现没有给出这种语法。
带有数据或集合的 jQuery.each(collection, callback(indexInArray, valueOfElement));
语法
jQuery.each( jQuery('#list option'), function(indexInArray, valueOfElement){
//your code here
});
OR
public class UserPrivacyData {
private static String password = null;
private final static String USER = "root";
final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
final String DATABASE_URL = "jdbc:derby://localhost:1527/USERPRIVACYDB;create=true;";
public void loadDB() throws SQLException {
try {
Class.forName(DRIVER); //load the driver
connection = DriverManager.getConnection(DATABASE_URL, USER, password);
statement = connection.createStatement();
} catch (Exception ex) {
ex.printStackTrace();
}
String sql = "insert into USERPRIVACYDB values('" + gender[rand.nextInt(2)] + "',"
+ " '" + privacyLevel[rand.nextInt(3)] + "', '" + age[rand.nextInt(3)] + "',"
+ " '" + presentEducationLevel[rand.nextInt(3)] + "',"
+ " '" + highestEducation[rand.nextInt(3)] + "', '"
+ everVictimOfCrimeString + "', "
+ " '" + crimeTypeString + "', "
+ " '" + shareWithThirdParty[rand.nextInt(2)] + "',"
+ " '" + reasonforChoiceofPrivacy[rand.nextInt(5)] + "')";
statement.executeUpdate(sql);
// }
}
}