使用 jQuery 选择上一个 td 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9830952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:21:54  来源:igfitidea点击:

using jQuery Select previous td text

jqueryjquery-selectors

提问by HaBo

Hi I have table in one tdi have text and in another tdi have Select Button

嗨,我在一个td 中有表格,我有文本,在另一个td 中,我有选择按钮

up on clicking the select button, i need to select the text of immediate previous td text.

单击选择按钮后,我需要选择前一个td 文本的文本

I know i can do it with .prev() but i am not familiar using that method can any one help me with it.

我知道我可以用 .prev() 做到这一点,但我不熟悉使用这种方法,任何人都可以帮助我。

my Script

我的脚本

function ReLoadMedications(mid) {
        var ProductName = "This shouled get the value of prev td text";
        var dea = $("input[type='button'][mid='"+mid+"']").attr('id');
        var mid = $("input[type='button'][mid='" + mid + "']").attr('mid');
        var form = $("input[type='button'][mid='" + mid + "']").attr('form');
        var href = $("input[type='button'][mid='" + mid + "']").attr('href');
        var con = false;
        var fax = false;
        alert(ProductName + "\n" + dea + "\n" + mid + "\n" + form + "\n" + href + "\n" + con + "\n" + fax);
}?

I have my code here at JS Fiddle, and also below:

我在JS Fiddle和下面有我的代码:

<div class="t-widget t-grid" id="TherapeuticAltGrid">
    <table cellspacing="0">
    <colgroup><col style="width:200px"><col></colgroup>
    <thead class="t-grid-header">
    <tr>
        <th class="t-header" scope="col" style="display:none">
            <a class="t-link" href="/BenefitsFormulary/HealthPlanInformation?PatientID=17&amp;MedicationID=6235&amp;MedicationName=LIPITOR%20EQ%2010MG%20BASE%20TABLET&amp;isFax=false&amp;form=TABLET&amp;TherapeuticAltGrid-orderBy=MedicationID-asc">Medication ID</a>
        </th>
        <th class="t-header" scope="col">
            <a class="t-link" href="/BenefitsFormulary/HealthPlanInformation?PatientID=17&amp;MedicationID=6235&amp;MedicationName=LIPITOR%20EQ%2010MG%20BASE%20TABLET&amp;isFax=false&amp;form=TABLET&amp;TherapeuticAltGrid-orderBy=Medication-asc">Medication</a>
        </th>
        <th class="t-header" scope="col">
            <span class="t-link">&nbsp;</span>
        </th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td style="display:none;">
            1147
        </td>
        <td>
            DACTINOMYCIN INJECTABLE 0.5MG/VIAL
        </td>
        <td class="t-last">
            <input type="button" value="Select" class="MedicationClicked button" mid="1147" id="0" form="INJECTABLE" onclick="ReLoadMedications(1147)">
        </td>
    </tr>
    <tr class="t-alt">
        <td style="display:none;">
            1439
        </td>
        <td>
            PROVENTIL SOLUTION EQ 0.5% BASE
        </td>
        <td class="t-last">
            <input type="button" value="Select" class="MedicationClicked button" mid="1439" id="0" form="SOLUTION" onclick="ReLoadMedications(1439)">
        </td>
    </tr>
    <tr>
        <td style="display:none;">
            1605
        </td>
        <td>
            EMETE-CON INJECTABLE EQ 50MG BASE/VIAL
        </td>
        <td class="t-last">
            <input type="button" value="Select" class="MedicationClicked button" mid="1605" id="0" form="INJECTABLE" onclick="ReLoadMedications(1605)">
        </td>
    </tr>
    </tbody>
    </table>
    <div class="t-grid-pager t-grid-bottom">
        <div class="t-status">
            <a class="t-icon t-refresh" href="/BenefitsFormulary/HealthPlanInformation?PatientID=17&amp;MedicationID=6235&amp;MedicationName=LIPITOR%20EQ%2010MG%20BASE%20TABLET&amp;isFax=false&amp;form=TABLET">Refresh</a>
        </div>
        <div class="t-pager t-reset">
            <a class="t-link t-state-disabled" href="#"><span class="t-icon t-arrow-first">first</span></a><a class="t-link t-state-disabled" href="#"><span class="t-icon t-arrow-prev">prev</span></a>
            <div class="t-numeric">
                <span class="t-state-active">1</span><a class="t-link">2</a><a class="t-link">3</a><a class="t-link">4</a><a class="t-link">5</a><a class="t-link">6</a><a class="t-link">7</a><a class="t-link">8</a><a class="t-link">9</a><a class="t-link">10</a><a class="t-link">...</a>
            </div>
            <a class="t-link" href="#"><span class="t-icon t-arrow-next">next</span></a><a class="t-link" href="#"><span class="t-icon t-arrow-last">last</span></a>
        </div>
        <div class="t-status-text">
            Displaying items 1 - 3 of 41
        </div>
    </div>
</div>

?

?

回答by Rafay

modify your inline code like

修改您的内联代码,例如

onclick="ReLoadMedications(1147,this)

and the function

和功能

function ReLoadMedications(mid,$this) {
    console.log($this);
    var pn = $($this).closest('td').prev('td').text();
    console.log(pn);
    //rest of your code 

DEMO

演示

回答by SpYk3HH

Easiest solution would be like he said, modify your inline code to except this

最简单的解决方案就像他说的那样,将您的内联代码修改为 this

Then in your code you can easily do

然后在你的代码中你可以很容易地做到

var ProductName = $(ele).parent().prev().text();

where ele, is your parameter in your function function ReLoadMedications(mid, ele)

其中 ele 是您函数中的参数 function ReLoadMedications(mid, ele)

Then you can also replace:

然后你也可以替换:

    var dea = $("input[type='button'][mid='"+mid+"']").attr('id');
    var mid = $("input[type='button'][mid='" + mid + "']").attr('mid');
    var form = $("input[type='button'][mid='" + mid + "']").attr('form');
    var href = $("input[type='button'][mid='" + mid + "']").attr('href');
    var con = false;
    var fax = false;

with

    var dea = $(ele).attr("id");
    var mid = $(ele).attr("mid");
    var form = $(ele).attr("form");
    var href = $(ele).attr("href");
    var con = false;
    var fax = false;