通过Jquery获取html元素的文本

时间:2014-07-04 06:37:48

标签: javascript html jquery-mobile

我正在尝试从列表中获取动态生成的文本,并在jquery移动应用程序中显示它的警告对话框,但它无法正常工作 这是我试过的

HTML

<center>
    <div class="preview ui-shadow swatch cus-bg-bdy" style="max-width:50em;text-align:left;">
        <div style="border-color: #DDDDDD !important;text-align:left;" class="ui-bar ui-bar-a">
                <h3>Customers</h3>

        </div>
        <div style="padding:1em;">
            <table>
                <tr>
                    <td>Name :</td>
                    <td>Dummy Name</td>
                </tr>
                <tr>
                    <td>Address :</td>
                    <td>Dummy Address</td>
                </tr>
            </table>
            <fieldset id="download_c_list" data-role="controlgroup" data-iconpos="left" data-filter="true" data-filter-placeholder="Search here..." data-count-theme="a">
                <label>
                    <input type="radio" name="customer">
                    <table data-role="table" data-mode="reflow" class="ui-responsive" style="font-weight: normal;">
                        <thead>
                            <th></th>
                        </thead>
                        <tbody>
                            <tr>
                                <td><font class="customer-name">Company-1</font>

                                </td>
                            </tr>
                            <tr>
                                <td><font class="customer-adress">Stradbroke Island.</font>

                                </td>
                            </tr>
                        </tbody>
                    </table>
                </label>
                <label>
                    <input type="radio" name="customer">
                    <table data-role="table" data-mode="reflow" class="ui-responsive" style="font-weight: normal;">
                        <thead>
                            <th></th>
                        </thead>
                        <tbody>
                            <tr>
                                <td><font class="customer-name">Shope-1</font>

                                </td>
                            </tr>
                            <tr>
                                <td><font class="customer-adress">address-1.</font>

                                </td>
                            </tr>
                        </tbody>
                    </table>
                </label>
                <label>
                    <input type="radio" name="customer">
                    <table data-role="table" data-mode="reflow" class="ui-responsive" style="font-weight: normal;">
                        <thead>
                            <th></th>
                        </thead>
                        <tbody>
                            <tr>
                                <td><font class="customer-name">customer-1</font>

                                </td>
                            </tr>
                            <tr>
                                <td><font class="customer-adress">Address-2.</font>

                                </td>
                            </tr>
                        </tbody>
                    </table>
                </label>
            </fieldset>
        </div>
    </div>
</center>

Jquery的

$("input[name=customer]:radio").change(function () {

alert($($($(this).siblings('label')).children("[name='custerm-name']")).html());
});

我想在提醒框中显示已点击商品的客户名称和地址,这里是小提琴http://jsfiddle.net/SzaBw/

3 个答案:

答案 0 :(得分:3)

尝试以下

$("input[name=customer]:radio").change(function () {
    var parent = $(this).parent();
    alert(parent.find(".customer-name").text());
    alert(parent.find(".customer-adress").text());
});

注意:您在customer-address班级

中输错了

答案 1 :(得分:1)

您需要将警报代码更新到此

alert($(this).closest(".ui-radio").find(".customer-name").text())
alert($(this).closest(".ui-radio").find(".customer-address").text())

这是上面代码的Fiddle

答案 2 :(得分:0)

试试这个:

$("#download_c_list input:radio").change(function () {
var label = $(this).prev();
var customerName = label.find('.customer-name').first();

var customerAdress = label.find('.customer-adress').first();
alert(customerName.html() + ' - ' + customerAdress.html());

});