weixin_33701294 2020-01-18 11:37 采纳率: 0%
浏览 35

jQuery-将文本附加到HTML

I have this ajax call that returns a short sentence which is for descriptive purposes for a user. Through the debugging console I can see that I am returning the correct data. My issue arises when I attempt to append this data to a html <p> tag. I have tried both .append and .text as I am using JQuery. I have had no luck so far, where am i going wrong?

HTML

<div>
  <p class="descriptionDisplay"> </p>
</div>

JQuery

    $('#scenarioDropdownList').change(function () {
        var scenarioId = $('#scenarioDropdownList option:selected').attr('id');
        getscenarioDescription(scenarioId);
        getData(scenarioId);
    });

    function getscenarioDescription(scenarioId) {
        $.ajax({
                    type: "GET",
                    url: 'https://localhost:44340/api/ScenarioDescriptors/GetScenarioDescriptions',
                    data: {scenarioId: scenarioId},
                    dataType: 'JSON',
                    success:function(data) {
                        $.each(data, function(key, val) {
                            console.log(val.scenarioDescription);
                            var descriptionText = val.scenarioDescription;

                            $('#descriptionDisplay').text(descriptionText); // This part isn't working correctly

                        })

                    }
                });  
    }

The output of console.log($('#descriptionDisplay').length) is 0.

  • 写回答

2条回答 默认 最新

  • 叼花硬汉 2020-01-18 11:41
    关注

    Try this, use .html

    $("p").html("Hello <b>world</b>!");
    
    评论

报告相同问题?