繁体   English   中英

Jquery如何检索/替换值<tr><td><span>价值?</span>

[英]Jquery how to retrieve / replace value in <span> <tr> <td> value?

有多个 TR TD,在用于格式化的表中给出了相同的类名,而没有给出 ID。

我想为 jQuery 寻求帮助。 如何检索/替换 span id = payment_currency_text, class="formdata 元素中的<td>内容?

1)要设置一个变量,存储为SGD ,从第二组 TR TD 中获取。
2) 将SGD替换为EUR

注:系统使用Jquery version = v1.4.2

我尝试使用以下语法但没有运气:

alert(jQuery("#payment_currency_text tr.find(.formdata)").val());
alert(jQuery("#payment_currency_text").next('.formdata').val());
alert(jQuery("#payment_currency_text tr td.formdata").val());
alert(jQuery("#payment_currency_text").find(".formdata").html());

...(tr td 孩子)

<tr>

  <td class="prompt" align="left">     
    Region* 
  </td>

  <td class="formdata" align="left">
     Asian 
  </td>
</tr>

<span id ="payment_currency_text"> <!--- There is a SPAN tag, with ID given  --->
<tr>
  <td class="prompt" align="left">     
     Payment currency* 
  </td>

  <td class="formdata" align="left">
     SGD   <!--- How i can set a variable to hold SGD, then replace to EUR?  --->
  </td>
</tr>
</span>

span内定位td.formdata这个:

要得到:

 var currency=$('#payment_currency_text .formdata').html();//currency will be SGD here

设置:

 $('#payment_currency_text .formdata').html('EUR');

如果你知道它总是一个特定的孩子,你可以使用 :nth-child() 选择器。

var setter = $("tr:nth-child(2) td:nth-child(2)").text();

$("tr:nth-child(2) td:nth-child(2)").text("EUR");

alert(setter)

这是一个小提琴http://jsfiddle.net/87V9v/

如果您只想更改 td 中的 html,请像这样使用

$(".formdata").html("some text or tags here");

如果你想在那些 td 中获取文本,请像这样使用

$(".formdata").each(function(){
  $(this).html();
 });

编辑

$("#payment_currency_text").find(".formdata").html()

$("#payment_currency_text").find(".formdata").html("some text or tags here");

您的 html 结构有问题,您不能将tr包含在 span 中。 tr应该是table的孩子。 所以相应地改变你的html。

 <table>
    <tr>

        <td class="prompt" align="left">
            Region*
        </td>

        <td class="formdata" align="left">
            Asian
        </td>
    </tr>


    <tr id="payment_currency_text">
        <td class="prompt" align="left">
            Payment currency*
        </td>

        <td class="formdata" align="left">
            SGD
            <!--- How i can set a variable to hold SGD, then replace to EUR?  --->
        </td>
    </tr>

</table>

然后你可以使用上面的代码来获取元素和值。

在 jQuery 中,您可以像这样替换元素的内部 HTML

$('.class-name').html("Some html in here");

但是,要小心,因为这将替换此名称的所有类的 HTML。

您可以使用text()获取数据(纯文本)以及用新数据替换现有数据。 如果您的替换是 HTML,则可以使用html()

var data=$('td.formdata').text();
alert("The value is"+data);
$('td.formdata').text("New Data");

您可以使用:

$("#payment_currency_text").text("your text");

或者

$("#payment_currency_text").html("your<b>text</b>");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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