gpt4 book ai didi

jquery - 如何将 .delegate 添加到此外部 .autocomplete 函数?

转载 作者:行者123 更新时间:2023-12-01 06:04:21 24 4
gpt4 key购买 nike

我今天刚刚了解了 .delegate,我知道我可以用于我想要的东西,但我不确定如何将它添加到这里。

添加的每一行都需要我能够使用.autocomplete,它们都使用相同的数据。

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript">

$(function() {

$('#field1').val("");
$('#field2').val("");
$('#field3').val("");
$('#field4').val("");


$("#field1").autocomplete({
source: "PRODUCT.ORDER.QUERY.php",
minLength: 2,
autoFocus: true,
select: function(event, ui) {
$('#field1').val(ui.item.field1);
$('#field2').val(ui.item.field2);
$('#field3').val(ui.item.field3);
$('#field4').val(ui.item.field4);

}
});
});

</script>

</head>
<body>
<button id="removeAll">Delete All</button>
<button id="addLine">New Line</button>
<hr>
<form action="<?php echo $PHP_SELF;?>" method="post" id="orderform">
<table width="90%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>FIELD 1</td>
<td>FIELD 2</td>
<td>FIELD 3</td>
<td>FIELD 4</td>
<td>QTY</td>
</tr>
</table> <hr>

<div id="orderForm">
<table class="orderLine" width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input name="field1" type="text" id="field1" size="15" tabindex="1"></td>
<td><input name="field2" type="text" id="field2" size="15"></td>
<td><input name="field3" type="text" id="field3" size="15"></td>
<td><input name="field4" type="text" id="field4" size="15"></td>
<td><input name="qty" type="text" id="qty" size="15" tabindex="2"></td>
<td><button class="removeLine">Delete</button></td>
</tr>
</table>
</div>
</form>
<!-- START OF THE JQUERY FUNCTION THAT ADDS THE NEW ORDER LINE -->

<script type="text/javascript">

$("#orderForm").delegate(".removeLine", "click", function () {
$(this).closest('.orderLine').remove();
});

<!-- This removes all newLine table rows -->
$("#removeAll").click(function () {
$('.orderLine').remove();
});

<!-- ADDS the 'newLine' table rows -->
$("#addLine").click(function () {

$('#orderForm').append('<table class="orderLine" width="90%" border="0" cellspacing="0" cellpadding="0"><tr><td><input name="field1" type="text" id="field1" size="15" tabindex="1"></td><td><input name="field2" type="text" id="field2" size="15"></td><td><input name="field3" type="text" id="field3" size="15"></td><td><input name="field4" type="text" id="field4" size="15"></td><td><input name="qty" type="text" id="qty" size="15" tabindex="2"></td><td><button class="removeLine">Delete</button></td></tr></table>');
});

</script>
</body>
</html>

工作 1/2 任性。不是我可以再次使用 .autocomplete 但它不正确。我再次填充所有字段。

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript">

$(function() {

$('.field1').val("");
$('.field2').val("");
$('.field3').val("");
$('.field4').val("");

$("body").delegate(".field1:not(:ui-autocomplete)","focus",function(){
$(this).autocomplete({
source: "PRODUCT.ORDER.QUERY.php",
minLength: 2,
autoFocus: true,
select: function(event, ui) {
$('.field1').val(ui.item.field1);
$('.field2').val(ui.item.field2);
$('.field3').val(ui.item.field3);
$('.field4').val(ui.item.field4);

}
});
});
});
</script>

</head>
<body>
<button id="removeAll">Delete All</button>
<button id="addLine">New Line</button>
<hr>
<form action="<?php echo $PHP_SELF;?>" method="post" id="orderform">
<table width="90%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>FIELD 1</td>
<td>FIELD 2</td>
<td>FIELD 3</td>
<td>FIELD 4</td>
<td>QTY</td>
</tr>
</table> <hr>

<div id="orderForm">
<table class="orderLine" width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input class="field1" type="text" size="15" tabindex="1"></td>
<td><input class="field2" type="text" size="15"></td>
<td><input class="field3" type="text" size="15"></td>
<td><input class="field4" type="text" size="15"></td>
<td><input class="qty" type="text" size="15" tabindex="2"></td>
<td><button class="removeLine">Delete</button></td>
</tr>
</table>
</div>
</form>
<!-- START OF THE JQUERY FUNCTION THAT ADDS THE NEW ORDER LINE -->

<script type="text/javascript">

$("#orderForm").delegate(".removeLine", "click", function () {
$(this).closest('.orderLine').remove();
});

<!-- This removes all newLine table rows -->
$("#removeAll").click(function () {
$('.orderLine').remove();
});

<!-- ADDS the 'newLine' table rows -->
$("#addLine").click(function () {

$('#orderForm').append('<table class="orderLine" width="90%" border="0" cellspacing="0" cellpadding="0"><tr><td><input class="field1" type="text" size="15" tabindex="1"></td><td><input class="field2" type="text" size="15"></td><td><input class="field3" type="text" size="15"></td><td><input class="field4" type="text" size="15"></td><td><input class="qty" type="text" size="15" tabindex="2"></td><td><button class="removeLine">Delete</button></td></tr></table>');
});

</script>
</body>
</html>

工作示例!

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript">

$(function() {

$('.field1').val("");
$('.field2').val("");
$('.field3').val("");
$('.field4').val("");

$("body").delegate(".field1:not(:ui-autocomplete)","focus",function(){
$(this).autocomplete({
source: "PRODUCT.ORDER.QUERY.php",
minLength: 2,
autoFocus: true,
select: function(event, ui) {
$(this).closest('tr').find('.field1').val(ui.item.field1);
$(this).closest('tr').find('.field2').val(ui.item.field2);
$(this).closest('tr').find('.field3').val(ui.item.field3);
$(this).closest('tr').find('.field4').val(ui.item.field4);

}
});
});
});
</script>

</head>
<body>
<button id="removeAll">Delete All</button>
<button id="addLine">New Line</button>
<hr>
<form action="<?php echo $PHP_SELF;?>" method="post" id="orderform">
<table width="90%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>FIELD 1</td>
<td>FIELD 2</td>
<td>FIELD 3</td>
<td>FIELD 4</td>
<td>QTY</td>
</tr>
</table> <hr>

<div id="orderForm">
<table class="orderLine" width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input class="field1" type="text" size="15" tabindex="1"></td>
<td><input class="field2" type="text" size="15"></td>
<td><input class="field3" type="text" size="15"></td>
<td><input class="field4" type="text" size="15"></td>
<td><input class="qty" type="text" size="15" tabindex="2"></td>
<td><button class="removeLine">Delete</button></td>
</tr>
</table>
</div>
</form>
<!-- START OF THE JQUERY FUNCTION THAT ADDS THE NEW ORDER LINE -->

<script type="text/javascript">

$("#orderForm").delegate(".removeLine", "click", function () {
$(this).closest('.orderLine').remove();
});

<!-- This removes all newLine table rows -->
$("#removeAll").click(function () {
$('.orderLine').remove();
});

<!-- ADDS the 'newLine' table rows -->
$("#addLine").click(function () {

$('#orderForm').append('<table class="orderLine" width="90%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input class="field1" type="text" size="15" tabindex="1"></td> <td><input class="field2" type="text" size="15"></td> <td><input class="field3" type="text" size="15"></td> <td><input class="field4" type="text" size="15"></td> <td><input class="qty" type="text" size="15" tabindex="2"></td> <td><button class="removeLine">Delete</button></td> </tr></table>');
});

</script>
</body>
</html>

最佳答案

假设字段有一个“field”类,这样的东西应该可以工作。

$("body").delegate(".field:not(:ui-autocomplete)","focus",function(){
$(this).autocomplete(options);
});

编辑:我没有意识到代码还有更多内容,所以我没有看到下面的 html。感谢罗斯兰指出这一点。重复的 ID 问题肯定会成为一个问题,但我发布的委托(delegate)代码应该是一个很好的起点,可以了解如何将插件动态应用到使用 ajax 添加的元素。之后,您只需克服重复的 id 问题即可。

编辑回复评论:

这条线和类似的线

$('.field1').val(ui.item.field1);

需要

$(this).closest('tr').find('.field1').val(ui.item.field1);
$(this).closest('tr').find('.field2').val(ui.item.field2);
$(this).closest('tr').find('.field3').val(ui.item.field3);
$(this).closest('tr').find('.field4').val(ui.item.field4);

编辑:添加一些优化:

var $tr = $(this).closest('tr');
$tr.find('.field1').val(ui.item.field1);
$tr.find('.field2').val(ui.item.field2);
$tr.find('.field3').val(ui.item.field3);
$tr.find('.field4').val(ui.item.field4);

关于jquery - 如何将 .delegate 添加到此外部 .autocomplete 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7959622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com