【问题标题】:Appending jQuery selector variable into a table将 jQuery 选择器变量附加到表中
【发布时间】:2014-03-15 12:11:23
【问题描述】:

这里是 jquery/编程新手。

我有一堆可以选择的文本框。选择该框后,我想在表中添加一个新行,并将我的文本框中的 h4 添加到新行的第一列中。变量有问题..

文本框如下所示:

<div class="boxed" data-price="7">
    <h4 class="boxTitle">Lemons</h4>
    <p>blahblahblah</p>
</div>

表格如下所示:

<tbody>
    <table class="table table-hover" id="theOrder">
    <tr>
        <th>Name</th>
        <th>Quantity</th>       
        <th>Price</th>
    </tr>
    <tr>
        <td>Product1</td>
        <td><input type="text" class="form-control" value="1"></td> 
    </tr>
    <tr>
        <td>Product2</td>
        <td><input type="text" class="form-control" value="1"></td> 
    </tr></table></tbody>

我的 jquery 看起来像这样,但我怎样才能让它打印我的变量 productName 而不是字符串?

$(document).ready(function() {

  $( ".boxed" ).click(function( event ) {
        var productName = $(this).find('.boxTitle');
        // highlighting - this works fine
$(this).toggleClass("highlightedBox");
        productName.toggleClass("boxTitleHl");

        // adds new row to the end of the table
        $('#theOrder').append('<tr><td class="newLine">productName</td><td><input type="text" class="form-control" value="1"></td></tr>');
    });
});

我什至尝试这样做,但它只是打印出 [object][object].. 我不知道为什么。需要把对象转成字符串吗?

$('#theOrder').append('<tr><td class="newLine">zzzz</td><td><input type="text" class="form-control" value="1"></td></tr>');
$(".newLine").text(productName);

做了一个小手!! http://jsfiddle.net/hBuck/

【问题讨论】:

  • 你能在这里创建/添加你的 jsfiddle 链接吗?它将帮助我们帮助您! :=)
  • 如何选择文本框?我想你指的是复选框

标签: jquery html dom jquery-selectors jquery-append


【解决方案1】:

您可以使用 jQuery text()html() 来获取标签内的内容

改变

var productName = $(this).find('.boxTitle');

var productName = $(this).find('.boxTitle').html();

var productName = $(this).find('.boxTitle').text();

也改变这一行

$('#theOrder').append('<tr><td class="newLine">'+productName+'</td><td><input type="text" class="form-control" value="1"></td></tr>');

试试这个代码

  $( ".boxed" ).click(function( event ) {
        var puddingN = $(this).find('.boxTitle');
        puddingName=puddingN.text();
        // highlight the box and the pudding name
        $(this).toggleClass("highlightedBox");
        puddingN.toggleClass("boxTitleHl");

        // TEST: changes title of table to pudding name..
        $(".orderTitle").text(puddingName);

        // adds new row to the end of the table
        //$('#theOrder').append('<tr><td class="newLine">EEEEK</td><td><input type="text" class="form-control" value="1"></td></tr>');
        // changes text to the name of the pudding but doesn't work :()
        //$(".newLine").text(puddingName);

        $('#theOrder').append('<tr><td class="newLine">'+puddingName+'</td><td><input type="text" class="form-control" value="1"></td></tr>');
    });

Fiddle Demo

【讨论】:

  • @user3315303 :很高兴为您提供帮助
【解决方案2】:

您需要将字符串与变量连接起来

var productName = $(this).find('.boxTitle').text();
 $('#theOrder').append('<tr><td class="newLine">'+productName+'</td><td><input type="text" 
class="form-control" value="1"></td></tr>');

【讨论】:

  • 您好,感谢您的回复。当我尝试更改为此时,与点击相关的任何功能都不起作用
猜你喜欢
  • 2017-08-15
  • 2011-05-06
  • 2018-09-01
  • 2020-08-03
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
相关资源
最近更新 更多