开发者

jquery How to remove an item with or without a comma/space?

Given a Input which can contain values like:

<input type="hidden" value="XXXXXXX" />

<input type="hidden" value="XXXXXXX, YYYYYYYY" />

<input type="hidden" value="XXXXXXX, YYYYYYYY, ZZZZZZZZZZ" />

I want to use jQuery to remove: XXXXXXX, but if XXXXXXX has a comma after (XXXXXXX,) I need the comma removed to.

This is what I have now:

uuid = XXXXXXX
.val(attachedUUIDs.replace(uu开发者_如何学Goid + ',', '' ));
.val(attachedUUIDs.replace(uuid, '' ));                                 

Ideas to do this cleanly? Thanks


This should do:

var uuid = 'XXXXXXX';
$('input').val(function (index, value) {
    return value.replace(new RegExp('\s*'+uuid+',?\s*', 'g'), '');
});


Split in a array the value, using the ',' like separator and then put the second element of the array as your value!! Can this help you?


Use a regex in the replace method and mark the comma as optional ?.

$('input:hidden').val(attachedUUIDs.replace(/XXXXXXX,?/, '' ));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜