如何通过点击一系列ID循环执行某些操作?

时间:2017-05-07 11:24:16

标签: javascript jquery arrays loops

        try
        {

            long personID = 0;
            using (SQLiteConnection sqlconnection = new SQLiteConnection("Data Source=" + DbPath + ";Version=3;"))
            {
                sqlconnection.Open();
                SQLiteCommand PersonCommand = sqlconnection.CreateCommand();
                SQLiteParameter personParam = new SQLiteParameter();
                PersonCommand.CommandText = "INSERT INTO Person('Name') VALUES(?)";
                PersonCommand.Parameters.Add(personParam);


                SQLiteCommand FileCommand = sqlconnection.CreateCommand();
                SQLiteParameter FileParam1 = new SQLiteParameter("@filename");
                SQLiteParameter FileParam2 = new SQLiteParameter("@filepath");
                SQLiteParameter FileParam3 = new SQLiteParameter("@personid");

                FileCommand.CommandText = "INSERT INTO file(FileName,FilePath,PersonID) VALUES(@filename,@filepath,@personid)";
                FileCommand.Parameters.Add(FileParam1);
                FileCommand.Parameters.Add(FileParam2);
                FileCommand.Parameters.Add(FileParam3);
                using (SQLiteTransaction _SQLiteTransaction = sqlconnection.BeginTransaction())
                {
                    for (int i = 0; i < persons.Count; i++)
                    {
                        personParam.Value = persons[i].Name;
                        PersonCommand.ExecuteNonQuery();
                        personID = sqlconnection.LastInsertRowId;

                        foreach (var item in Files.Where(f => f.PersonID == personID))
                        {
                            FileParam1.Value = item.FileName;
                            FileParam2.Value = item.FilePath;
                            FileParam3.Value = item.PersonID;
                            FileCommand.ExecuteNonQuery();
                        }
                    }
                    _SQLiteTransaction.Commit();
                    form.Progress();
                }
                sqlconnection.Close();
            }
            result = 1;

        }
        catch (Exception e)
        {
            result = 0;
            throw;
        }

我试图在点击每个元素时发生一些事情,但只触发最后一个元素。我该怎么做?

4 个答案:

答案 0 :(得分:0)

试试这个:

var topOptions = ["#m1", "#m2", "#m3", "#m4", "#m5", "#m6"];
$.each(topOptions,function(i,item){
$(item).animate({backgroundColor: '#2c3e50'}, 150);
});

答案 1 :(得分:0)

$(this).animate .... 

而不是

$(topOptions[i]).animate ....

答案 2 :(得分:0)

 $("a").click(function(){
 var topOptions = ["#m1", "#m2", "#m3", "#m4", "#m5", "#m6"];
 if(jQuery.inArray( $(this).attr("id"),topOptions )>-1){
 $(this).animate({backgroundColor: '#2c3e50'}, 150);
 }
 });

请注意,我使用a作为选择器,如果它是div或其他更改此

  

$(“一)

我想你想要这个函数jQuery.inArray检查你在topOtions数组中找到你点击的id

答案 3 :(得分:0)

注意,您无法使用jQuery动画制作backgroudColor动画。您可以使用css tansition为背景颜色设置动画。这是您可以使用的解决方案

&#13;
&#13;
var ids = ['#id1','#id2','#id3','#id4','#id5'];

ids.forEach(function(id){
	$(id).click(function(e){
		$(this).css('backgroundColor', '#2c3e50')
	});
});
&#13;
li{
	-webkit-transition: background 0.5s linear;
-moz-transition: background 0.5s linear;
-ms-transition: background 0.5s linear;
-o-transition: background 0.5s linear;
transition: background 0.5s linear;
}
&#13;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JavaScript</title>
</head>
<body>
<ul>
	<li id="id1">id1</li>
	<li id="id2">id2</li>
	<li id="id3">id3</li>
	<li id="id4">id4</li>
	<li id="id5">id5</li>	
	</ul>	
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>

</body>
</html>
&#13;
&#13;
&#13;

要了解您的for循环为何使用最后一个元素,请查看此答案JavaScript closure inside loops – simple practical example