Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ajax中,数据正在提交,但数据未在字段中显示_Javascript_Html_Ajax - Fatal编程技术网

Javascript 在ajax中,数据正在提交,但数据未在字段中显示

Javascript 在ajax中,数据正在提交,但数据未在字段中显示,javascript,html,ajax,Javascript,Html,Ajax,这是我的html <script> function get_customer() { $("#customer").html(''); $("#customer").html('<option value="0">SELECT CUSTOMER</option>'); $.ajax({ url: '<?=base_url();?>Drop_downs/get_custome

这是我的html

<script>
function get_customer() {

        $("#customer").html('');
        $("#customer").html('<option value="0">SELECT CUSTOMER</option>');
        $.ajax({
            url: '<?=base_url();?>Drop_downs/get_customer/',
            type: 'POST',
            dataType: 'html',
            success: function(response) {
                $("#customer").append(response);
                console.log(response);
            }

        });

}</script>

Response不是HTML节点,因此不能附加它。请改用$'customer'.html

您似乎收到了选项标记html代码的响应,所以您必须使用$'customer'.htmlresponse

如果不知道“反应”是什么,我们将无法提供具体的帮助。尽管如此,以下内容应能帮助您:

<select name="customer" id="customer" class="form-control select2" required tabindex="1" onFocus="get_customer()">
          <option>SELECT CUSTOMER</option>
</select>
添加硬编码值

要在下拉框中显示文本1,并且值Value1是将从表单提交的内容时向其添加新选项,请执行以下操作:

<script>
function get_customer() {
     $("#customer").html('');
     $("#customer").html('<option value="0">SELECT CUSTOMER</option>');
     $.ajax({
            url: '<?=base_url();?>Drop_downs/get_customer/',
            type: 'POST',
            dataType: 'json',
            success: function(response) {
                $("#customer").html(response);
                console.log(response);
            }
        });

}</script>
从对象添加选项

如果有一个对象希望使用特性名称作为选项的值,并使用特性值作为要显示的文本,则可以执行以下操作:

var select = document.getElementById("customer");
select.options[select.options.length] = new Option('Text 1', 'Value1');
请注意,虽然上面是一个对象,但它通常被错误地称为关联数组,即使从技术上讲它不是。它可以作为对象(例如myobject.ValueA)访问,也可以像数组一样访问,例如myobject['ValueA']

现在,这段代码是通用的,有望帮助您实现所需的功能。但是,如果你能更新你的问题的更多细节,比如回复的内容,我将能够为你提供更多的帮助和解决方案

可能的JQuery解决方案:

如果您使用的是JQuery,那么它会变得更简单:

var myobject = {
    ValueA : 'Text A',
    ValueB : 'Text B',
    ValueC : 'Text C'
};

var select = document.getElementById("customer");
for(index in myobject) {
    select.options[select.options.length] = new Option(myobject[index], index);
}
如果要从项目集合中添加选项,可以执行以下操作:

$('#customer').append($('<option>', {
    value: 1,
    text: 'My option'
}));

请提供console.logresponse; 由于我不知道您的回答,我假设您在“customer_name”中使用了客户名称。请尝试以下代码

    var myOptions = {
                     val1 : 'Blue',
                     val2 : 'Orange'
                    };
var mySelect = $('#customer');
$.each(myOptions, function(val, text) {
    mySelect.append(
        $('<option></option>').val(val).html(text)
    );
});

你的回答是什么你的回答也是对的,伙计,我们需要知道你的回答是什么。在询问问题之前,应该为新来者提供更清晰的说明。但这将覆盖他在其中获得的任何数据。您的解决方案将覆盖其中已有的一个默认选项。
<script>
function get_customer() {
     $("#customer").html('');
     $("#customer").html('<option value="0">SELECT CUSTOMER</option>');
     $.ajax({
            url: '<?=base_url();?>Drop_downs/get_customer/',
            type: 'POST',
            dataType: 'json',
            success: function(response) {
                var i = 0;
                while (i < data.length)
                    {

                        $('<option>').text(data[i].customer_name).appendTo('#customer');
                        i++;
                    }
            }
        });

}</script>