简体   繁体   English

如何使用Jquery将文本附加到data-attribute

[英]How to append text to data-attribute using Jquery

I have modal window and data attribute on it. 我有模态窗口和数据属性。

<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>|

and this is my jQuery function 这是我的jQuery函数

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

I know i can set this element value like this: 我知道我可以像这样设置这个元素值:

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

My question is: how to append text to this attribute value so i will have: 我的问题是:如何将文本附加到此属性值,以便我将:

"Are you sure you want to submit the request “您确定要提交请求吗?
from" + myAppendedText. 来自“+ myAppendedText。

I tried with a.val().append() and a.val().join() but it did not work. 我尝试使用a.val()。append()和a.val()。join()但它没有用。 I also checked and tried something like this how to append text to an attribute like Value but it did not work too. 我还检查并尝试了类似这样的方法如何将文本追加到像Value这样的属性,但它也没有用。 Any help ? 有帮助吗?

There is no append methods for the data attributes, but you can use like this 数据属性没有附加方法,但您可以像这样使用

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

You are almost there! 你快到了!

Just you cached it now take a new var and set it again like: 只是你缓存它现在采取一个新的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM