문제

I currently have a jQuery function where I perform an AJAX request. Upon success I see that the returned data is correct but when I try and change the data-id="new" to the new response, it does absolutely nothing.

I am loading the data-id="new" div into the DOM after initial page load.

I am able to navigate all the way to .each-requirement and change the HTML within with the response but unable to change the .each-requirement data-id.

HTML:

<div class="dashboard-container-toolbar dashboard-requirement-toolbar center">
<div class="select2-container requirements-select select2-allowclear" id="s2id_add-essential-requirement">
    <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabindex="-1">
        <span class="select2-chosen">CSS</span><abbr class="select2-search-choice-close"></abbr>   
        <span class="select2-arrow"><b></b></span></a><input class="select2-focusser select2-offscreen" type="text" id="s2id_autogen1"></div>
        <select id="add-essential-requirement" class="requirements-select select2-offscreen" data-placeholder="Add Essential Requirement" tabindex="-1">
            <option></option>
            <option value="cmYiaONlNtw5GP11-e">CAD</option>
            <option value="zdttiUMKAR0sKlSu-e">CSS</option>
            <option value="4MzgJIEyaL2vizhQ-e">Excel Spreadsheets</option>
            <option value="j70U4vt5kWm7wLjB-e">Fireworks</option>
            <option value="7nCu8QIIJQzAQAy3-e">Git</option>
            <option value="1bZM8poaikJRLo5P-e">HTML</option>
        </select>
</div>
<div class="requirement-container sans-serif center"><br><br>
<div class="each-requirement overflow-hidden" data-id="new">
    <div style="margin-left:80px" class="name float-left" data-id="zdttiUMKAR0sKlSu-e">CSS</div>
    <span style="margin-left:-37px;" id="skill-level-0" class="skill-level color-blue center sans-serif">Beginner</span>
    <div style="margin: 20px auto 0" class="slide ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all" id="slider-0" aria-disabled="false">
        <div class="inner-slider-marker"></div>
        <a class="ui-slider-handle ui-state-default ui-corner-all" href="#" style="left: 0%;"></a>
    </div>
</div>

jQuery:

$(document).on('change', '.requirements-select', function(event){
    event.preventDefault();
    var $this = $(this);

    var name = $this.select2('data').text;
    var str = $this.val();

    $('.initial-r-load').remove();
    $this.parents('.dashboard-requirement-toolbar').siblings('.requirement-container').append('<div class="each-requirement overflow-hidden" data-id="new"></div>');
    $this.parents('.dashboard-requirement-toolbar').siblings('.requirement-container').children('.each-requirement:last').addSlide(name, str);

    $.ajax({
        type: "POST",
        url: base_url + "job/add_requirement",
        dataType: "text",
        data: {
            skl_name: name,
            skl_str: str,
            j_str: window.location.pathname.split('/').pop()
        },
        cache:false,
        success: 
        function(data){
            var response = data;
            /* THIS WORKS */ $this.parents('.dashboard-requirement-toolbar').siblings('.requirement-container').children('.each-requirement:last').html(response)
            /* THIS DOESN'T WORK */ $this.parents('.dashboard-requirement-toolbar').siblings('.requirement-container').children('.each-requirement:last').data('id', response)          
        }
    });

    return false;

});

Any help is appreciated. Thanks.

도움이 되었습니까?

해결책

Just copying the comments, data method does not set or change the attribute value, it amends the internal data structure of the element, which is not displayed as changed data-* attribute.

So, if you need a markup change then use .attr('data-id', 'whatever-value') instead. Otherwise use .data() method, as you do, but check the correctness with picking up the value back, i.e. console.log(el.data('id')).

P.S.: As a side note, I'd recommend you to use closest() method instead of parents(). It will rapidly pick up the first matched parent element, instead of selecting all matched parent elements.

다른 팁

Try to modify code with the following :

$this.parents('.dashboard-requirement-toolbar')
     .siblings('.requirement-container')
     .children('.each-requirement:last')
     .attr('data-id', response)

This should work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top