自动填充表格javascript没有用

时间:2017-12-28 19:12:19

标签: javascript jquery ajax

来自" request.done ...."的最后2行没有工作......没有任何事情发生......其他一切都在发挥作用

这是我的剧本:

$(document).ready(function () {

    $('#retrieve-resources').click(function () {
        var displayResources = $('#display-resources');

        displayResources.text('Loading data from JSON source...');
        var url = "--------------------".concat(document.getElementById('title').value);

        $(document.getElementById("writer").value = '');
        $(document.getElementById("director").value = '');
        $(document.getElementById("actors").value = '');
        $(document.getElementById("year").value = '');
        $(document.getElementById("plot").value = '');
        $(document.getElementById("poster").value = '');


        var request = $.ajax({

            url: url,
            type: "GET",
            dataType: "json"

        });

        request.done(function (result) {

            $(document.getElementById("writer").value = result.Writer.replace(new RegExp("\\s?\\(.*?\\)", "g"), ''));
            $(document.getElementById("director").value = result.Director.replace(new RegExp("\\s?\\(.*?\\)", "g"), ''));
            $(document.getElementById("actors").value = result.Actors.replace(new RegExp("\\s?\\(.*?\\)", "g"), ''));
            $(document.getElementById("year").value = result.Year);
            $(document.getElementById("poster").value = result.Poster.toString());
            displayResources.html('Form is filed!');
        });


    });
});

2 个答案:

答案 0 :(得分:0)

要使用jQuery设置元素的值,您应该使用.val函数。例如,不要使用下面的代码将id为writer的元素的值设置为空字符串:

$(document.getElementById("writer").value = '');

您应该使用此jQuery选择器和.val

$('#writer').val('');

答案 1 :(得分:0)

你不应该在每一行周围$()$()的参数应该是选择器,HTML字符串或函数。在尝试解析您分配的值时,您可能会收到错误。

$(document).ready(function () {
    $('#retrieve-resources').click(function () {
        var displayResources = '#display-resources';

        displayResources.text('Loading data from JSON source...');
        var url = "--------------------".concat(document.getElementById('title').value);

        document.getElementById("writer").value = '';
        document.getElementById("director").value = '';
        document.getElementById("actors").value = '';
        document.getElementById("year").value = '';
        document.getElementById("plot").value = '';
        document.getElementById("poster").value = '';

        var request = $.ajax({
            url: url,
            type: "GET",
            dataType: "json"
        });

        request.done(function (result) {
            document.getElementById("writer").value = result.Writer.replace(new RegExp("\\s?\\(.*?\\)", "g"), '');
            document.getElementById("director").value = result.Director.replace(new RegExp("\\s?\\(.*?\\)", "g"), '');
            document.getElementById("actors").value = result.Actors.replace(new RegExp("\\s?\\(.*?\\)", "g"), '');
            document.getElementById("year").value = result.Year;
            document.getElementById("poster").value = result.Poster.toString();
            displayResources.html('Form is filed!');
        });
    });
});