在Javascript中调用类名时不起作用?

在Javascript中调用类名时不起作用?,javascript,php,jquery,autocomplete,psql,Javascript,Php,Jquery,Autocomplete,Psql,我有一个网页,其中有一个具有不同元素的表单,其中还有一个文本字段,它从数据库中搜索名称并以下拉方式显示 下面还有一个字段,它是一个按钮,通过它我可以添加新的文本字段,同上 在新添加的文本字段中,我想要与上面相同的自动完成功能 我已经给出了所有正确的类名,但它无法获取名称并以自动完成的方式显示 NewUser.php NewEntryValues.js 我用它作为参考试试这样的东西。仔细检查代码。这是我以前代码的一部分,它工作得非常好。我在文本框中得到了值,但正如您在我的php代码中所看到的,我对

我有一个网页,其中有一个具有不同元素的表单,其中还有一个文本字段,它从数据库中搜索名称并以下拉方式显示

下面还有一个字段,它是一个按钮,通过它我可以添加新的文本字段,同上

在新添加的文本字段中,我想要与上面相同的自动完成功能

我已经给出了所有正确的类名,但它无法获取名称并以自动完成的方式显示

NewUser.php

NewEntryValues.js


我用它作为参考

试试这样的东西。仔细检查代码。这是我以前代码的一部分,它工作得非常好。我在文本框中得到了值,但正如您在我的php代码中所看到的,我对代码进行了注释,以在下拉列表中获得值

您的php文件(autocomplete.php)


将文本输入附加到DOM后,需要初始化文本输入。当使用
.newentry
作为选择器时,它会获取当前属于DOM的元素,但不是稍后附加的元素。

您正在
上初始化自动完成。newentry
而您的
.newentry
还不是DOM的一部分

// Initialize autocomplete with custom appendTo:
$('.newentry').autocomplete({
     lookup: peopleArray
});
.newentry
出现在
$(添加按钮)中的图片中。在
prepend()时单击()
包装中的
.newentry

wrapper
中添加
.newentry
后初始化您的
autocomplete

已更新

在NewUser.php中

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var peopleArray = $.map(partnames, function (value, key) { 
        return { value: value, data: key }; 
    });

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increme

            $(wrapper).prepend('<br><div style="margin-left:50px;"><center><div class="form-group"> <label class=" control-label" for="textinput" style="margin-left:327px;">Name: </label> <div > <input id="partner_names[]" name="partner_names[]" type="text" placeholder="Enter Full Name" class="form-control input-md newentry" style="margin-top: -25px;margin-left: 403px;width: 241%;">  </div>  <a href="#" class="remove_field"><img src="images/del24.png" style="margin-left: 810px; margin-top: -81px;"></a></a></div>'); //add input box

            //Initialize autocomplete here when it has become the part of the DOM
            $('.newentry').autocomplete({
                lookup: peopleArray
            });
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
$(文档).ready(函数(){
var max_fields=10;//允许的最大输入框数
var wrapper=$(“.input_fields_wrapp”);//字段包装器
var add_button=$(“.add_字段_button”);//添加按钮ID
var peopleArray=$.map(部件名、函数(值、键){
返回{value:value,data:key};
});
var x=1;//初始文本框计数
$(添加按钮)。单击(函数(e){//在添加输入按钮上单击
e、 预防默认值();
如果(x名称:”);//添加输入框
//当自动完成成为DOM的一部分时,在此处初始化自动完成
$('.newentry')。自动完成({
查找:peopleArray
});
}
});
$(包装器)。在(“单击“,”.remove_字段)上,函数(e){//用户单击remove text
e、 preventDefault();$(this).parent('div').remove();x--;
})
});

您的自动完成代码在哪里?我更新了上面的问题,请参见您正在调用
autocomplete()
.newentry
,而它不是DOM的一部分。你应该在
$(wrapper.prepend()
之后调用
autoComplete
。这与这个问题有什么关系?php
是如何以及何时出现的?@rorymcrossan也许不是你说的,但他正试图实现我所拥有的同样的目标。这是从数据库获取数据并在下拉列表中自动完成?@sam请参考上面的代码,它是用php、javascript和html@JimmySehgal我以前这么做过,但是我的数据库有数千个值,所以每次我都无法向数据库发出请求,所以我,把它拉到一个文件中,然后我正在搜索它你能添加它并给我看吗,我没有it@aiffin检查上面的代码。您需要在
$(添加按钮)内初始化
自动完成
。单击()。如果您还有任何疑问,请告诉我。
var partnames={"19":"ABCD","42":"group","103":"cv","104":"name_to_1","105":"livetest","106":"live2"}
<?php 
$searchTerm = $_GET['term'];
$query = mysql_query("select name from test where name LIKE '%".$searchTerm."%' ORDER BY name ASC"); // Run your query
/*echo '<select name="taskname">'; // Open your drop down box

// Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
echo '</select>';*/
while ($row = mysql_fetch_array($query)) {
    $data[] = $row['name'];
}

//return json data
echo json_encode($data);
?>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
        $(function() 
        {
$( "#skills" ).autocomplete({
    source: 'autocomplete.php'
});
});
</script>
<td><input id="skills" name="taskname"></td></tr>
    <label for="skills">Task Name: </label>
    <input id="skills">
// Initialize autocomplete with custom appendTo:
$('.newentry').autocomplete({
     lookup: peopleArray
});
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var peopleArray = $.map(partnames, function (value, key) { 
        return { value: value, data: key }; 
    });

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increme

            $(wrapper).prepend('<br><div style="margin-left:50px;"><center><div class="form-group"> <label class=" control-label" for="textinput" style="margin-left:327px;">Name: </label> <div > <input id="partner_names[]" name="partner_names[]" type="text" placeholder="Enter Full Name" class="form-control input-md newentry" style="margin-top: -25px;margin-left: 403px;width: 241%;">  </div>  <a href="#" class="remove_field"><img src="images/del24.png" style="margin-left: 810px; margin-top: -81px;"></a></a></div>'); //add input box

            //Initialize autocomplete here when it has become the part of the DOM
            $('.newentry').autocomplete({
                lookup: peopleArray
            });
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});