1

当用户更改活动标题元素并触发 $.ajax 调用时,我试图获取 SPAN 的值。我有这个代码:http ://pastebin.com/rxQRSADQ我在更改事件中得到的值是包含在 SPAN 中的值,类:selected-center-open-ticket-label 但我只需要数字“1”而不是整个值,意思是“票:1”。有什么可以帮助我的吗?

4

2 回答 2

2

regexp匹配的数字怎么样?

var string = "Ticket11: 1".match(/\d+$/)[0];
alert(string);

这样你就可以传递任何字符串,就像我的例子一样,它会返回最后一个数字。

\d匹配数字。
+匹配一个或多个。
$匹配字符串的结尾。

匹配演示

于 2012-07-27T03:25:21.980 回答
1

如果它始终是“票:”,那么您可以使用substr(8).

编辑:

演示:http: //jsfiddle.net/SO_AMK/6dkh6/

jQuery:

$('.selected-center-open-ticket').click(function() {
    alert($(this).find(".selected-center-open-ticket-label").text().substr(8));
});​

HTML:

<div id="tickets-accordion">
    <div class="selected-center-open-ticket">
        <a href="#"><img src="/assets/img/admin/icon/icon2.png" alt="Close" title="Close" class="max-icon" width="21" height="17" /></a>
        <img src="/assets/img/admin/foto-2.jpg" alt="Username" title="Username"
        class="avatar" />
        <span class="selected-center-open-ticket-label">Ticket: 1</span>
        <span class="selected-center-open-ticket-store">La Trinidad</span>
        <span class="selected-center-open-ticket-user">Reynier Perez Mira</span>
    </div>
    <div class="selected-center-open-ticket">
        <a href="#"><img src="/assets/img/admin/icon/icon2.png" alt="Close" title="Close" class="max-icon" width="21" height="17" /></a>
        <img src="/assets/img/admin/foto-2.jpg" alt="Username" title="Username"
        class="avatar" />
        <span class="selected-center-open-ticket-label">Ticket: 2</span>
        <span class="selected-center-open-ticket-store">Boleíta</span>
        <span class="selected-center-open-ticket-user">Reynier Perez Mira</span>
    </div>
</div>​
于 2012-07-27T03:24:16.097 回答