![](/img/trans.png)
[英]How to find the highest data-attribute with js/jquery and append a div to it
[英]How to append text to data-attribute using 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()但它沒有用。 我還檢查並嘗試了類似這樣的方法如何將文本追加到像Value這樣的屬性,但它也沒有用。 有幫助嗎?
數據屬性沒有附加方法,但您可以像這樣使用
var oldData = $(this).data('additionalinfo');
$(this).data('additionalinfo', oldData + " new Text");
你快到了!
只是你緩存它現在采取一個新的var並再次設置如下:
$('#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.
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.