输入字段不会停止使用jQuery和setTimeout()重新填充

时间:2017-03-14 23:39:31

标签: javascript jquery html settimeout

我在窗口小部件中填充输入值,其中包含在单击输入字段时通过URL传递的值。因为窗口小部件需要比页面的其余部分加载更长,所以我使用setTimeout()和jQuery来完成此任务:

TaskInfo.html

    //tasks hold of 15K records around
    <div ng-options="task.name as task.taskId for task in tasks> </div>

TaskController.js

$http.get('taskDetails').success(function(response){
$scope.tasks=response;
});

输入字段确实填充正确,问题是任何时候点击其他任何地方,或者如果重新点击该字段,数据将被再次覆盖,删除可能已添加到该字段的任何内容。

我试图通过在条件满足时说var email = getUrlParameter('email'); console.log(email); var description = getUrlParameter('description'); console.log(description); // Contents of the two input fields I wish to populate var first = $('#ticketSubmissionForm').contents().find('input[name="email"]').val(); var second = $('#ticketSubmissionForm').contents().find('textarea[name="description"]').val(); console.log(first); console.log(second); var timerone, timertwo; timerone = setTimeout(function(){ $('#ticketSubmissionForm').contents().find('label.Form-fieldContainer.u-block').click(function(){ if (!first) { var change = $('#ticketSubmissionForm').contents().find('input[name="email"]').val(email); console.log(change); clearTimeout(timerone); } }); }, 4000); timertwo = setTimeout(function(){ $('#ticketSubmissionForm').contents().find('textarea[name="description"]').click(function(){ if (!second){ var change2 = $('#ticketSubmissionForm').contents().find('textarea[name="description"]').val(description); console.log(change2); clearTimeout(timertwo); } }); }, 4000); clearTimeout(timerone)来解决这个问题,但这不会改变任何事情。

谁能看到我的问题是什么?我希望在第一次点击填充字段后停止填充字段

2 个答案:

答案 0 :(得分:2)

您错过了重新启动Imports Excel = Microsoft.Office.Interop.Excel Imports System.Data.OleDb '~~> Define your Excel Objects Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim xlApp As New Excel.Application 'Dim xlWorkBook As Excel.Workbook 'Dim xlWorkSheet As Excel.Worksheet Dim strConn As String Dim da As OleDbDataAdapter Dim ds As New DataSet Dim dao_dbE As dao.DBEngine Dim dao_DB As DAO.Database Dim strFirstSheetName As String dao_dbE = New dao.DBEngine dao_DB = dao_dbE.OpenDatabase("C:\Users\Excel\Desktop\Coding\DOT.NET\Samples VB\Import into DataGrid from Excel & Export from DataGrid to Excel #2\Book3.xls", False, True, "Excel 8.0;") strFirstSheetName = dao_DB.TableDefs(0).Name strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Users\Excel\Desktop\Coding\DOT.NET\Samples VB\Import into DataGrid from Excel & Export from DataGrid to Excel #2\Book3.xls;Extended Properties=""Excel 8.0;""" da = New OleDbDataAdapter("SELECT * FROM [" & _ strFirstSheetName & "]", strConn) da.TableMappings.Add("Table", "Excel") da.Fill(ds) DataGridView1.DataSource = ds.Tables(0).DefaultView da.Dispose() dao_DB.Close() End Sub End Class first个变量:

second

答案 1 :(得分:0)

如果要确保dom准备好,可以使用$(document).ready()。

	var email = 'email';//getUrlParameter('email');
	var description = 'description';//getUrlParameter('description');

	// Contents of the two input fields I wish to populate
	var first = $('#ticketSubmissionForm').find('input[name="email"]').val();
	var second = $('#ticketSubmissionForm').find('textarea[name="description"]').val();

	$(document).ready(function() { // or just $(function() {...});
		$('#ticketSubmissionForm').find('label.Form-fieldContainer.u-block').click(function(){
			if (!first) {
				$('#ticketSubmissionForm').find('input[name="email"]').val(email);
                first = email;
			}
		});
		$('#ticketSubmissionForm').find('textarea[name="description"]').click(function(){
			if (!second){
				 $('#ticketSubmissionForm').find('textarea[name="description"]').val(description);
                second = description;
			}
		});
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="ticketSubmissionForm">
<label class="Form-fieldContainer u-block">label</label>
  <input type="text" name="email" />
  <textarea name="description"></textarea>
</form>

顺便说一下,.content()调用不是必需的,因为您没有选择文本节点或注释。你可以直接使用.find()

除了更改第一个和第二个值之外,您还可以使用jquery .one()方法,该方法在第一次调用后删除事件处理程序

var email = 'email'; //getUrlParameter('email');
var description = 'desription'; //getUrlParameter('description');

// Contents of the two input fields I wish to populate
var first = $('#ticketSubmissionForm').find('input[name="email"]').val();
var second = $('#ticketSubmissionForm').find('textarea[name="description"]').val();

$(document).ready(function() { // or just $(function() {...});
  $('#ticketSubmissionForm').find('label.Form-fieldContainer.u-block').one('click', function(){
    if (!first) {
      $('#ticketSubmissionForm').find('input[name="email"]').val(email);
    }
  });
  $('#ticketSubmissionForm').find('textarea[name="description"]').one('click', function(){
    if (!second){
       $('#ticketSubmissionForm').find('textarea[name="description"]').val(description);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="ticketSubmissionForm">
<label class="Form-fieldContainer u-block">label</label>
  <input type="text" name="email" />
  <textarea name="description"></textarea>
</form>