AJAX成功后无法设置数据ID?

时间:2014-04-15 10:47:37

标签: javascript jquery html ajax custom-data-attribute

我目前有一个jQuery函数,我执行一个AJAX请求。成功后,我看到返回的数据是正确的,但是当我尝试将data-id="new"更改为新响应时,它绝对没有任何内容。

我在初始页面加载后将data-id="new" div加载到DOM中。

我可以一直导航到.each-requirement并使用响应更改HTML,但无法更改.each-requirement数据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;

});

感谢任何帮助。感谢。

2 个答案:

答案 0 :(得分:2)

只是复制注释,data方法不会设置或更改属性值,它会修改元素的内部数据结构,不显示为已更改{{1} } attribute。

因此,如果您需要更改标记,请改用data-*。否则,请像您一样使用.attr('data-id', 'whatever-value')方法,但请检查正确性,然后选择值,即.data()

P.S。:作为旁注,我建议您使用closest()方法而不是console.log(el.data('id'))。它将快速获取第一个匹配的父元素,而不是选择所有匹配的父元素。

答案 1 :(得分:0)

尝试使用以下内容修改代码:

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

这应该有用。