2015-09-28 45 views
1

我有它的模態窗口和數據屬性。如何附加文本到數據屬性使用jquery

<a href="#" data-toggle="modal" data-target="#modal-request-confirmation" id="modalWindow" value="Send" 
         class="btn btn-default" data-form="requestPto" data-html="true" 
         **data-additionalinfo="Are you sure you want to submit the request <br /> from"** > 
        Request 
        </a>| 

,這是我的jQuery功能

$('#modalWindow').on('click',function(){ 
     var startDate = new Date($("#StartDate").val()); 
     var endDate = new Date($("#EndDate").val()); 
     var a = $(this).data('additionalinfo'); 
    }); 

我知道我可以這樣設置這個元素值:

$(this).data('additionalinfo', 'my appended text/value/etc'); 

我的問題是:如何將文本追加到該屬性值所以我將有:

「您確定要提交請求
從「+ myAppendedText。我嘗試過使用a.val()。append()和a.val()。join(),但它不起作用。我也檢查過,並嘗試過這樣的how to append text to an attribute like Value,但它不起作用。任何幫助?

回答

2

沒有爲數據屬性沒有追加的方法,但你可以使用這樣

var oldData = $(this).data('additionalinfo'); 
$(this).data('additionalinfo', oldData + " new Text"); 
+1

它的工作原理,感謝你回答我這麼快。 –

1

你就要成功了!

只要你緩存現在需要一個新的變種,並重新設置它像:

$('#modalWindow').on('click',function(){ 
     var startDate = new Date($("#StartDate").val()); 
     var endDate = new Date($("#EndDate").val()); 
     var a = $(this).data('additionalinfo'); // cache it here 
     var newA = a + " new Text to put." // add those two 
     $(this).data('additionalinfo', newA); // now set it again. 
    }); 
+0

謝謝,我剛剛做完了。你知道任何簡短的方式來顯示一些dateTime格式(MM/dd/yyyy)比dateTime.getMonth()+ 1 + dateTime.getDate()+ dateTime.getFullYear()? –