【问题标题】:javascript passing values dynamically to a method jqueryjavascript动态地将值传递给方法jquery
【发布时间】:2013-04-29 03:17:13
【问题描述】:
$(document).bind('pageinit', function () {
    var vendor_id = $.urlParam('vendor_id');
    $.ajax({
        type: "GET",
        url: "http://testservice/testmenu",
        data: {
            vendor_id: vendor_id
        },
        error: function () {
            alert("Could not get the menu : " + url);
        },
        success: function parseXml(xml) {
            var jsonData = $.parseJSON(xml);
            $(jsonData).each(function (index, post) {
                $(post).each(function (index, row) {
                    var finalString = [];
                    for(var index = 0; index < row.menu.length; index++) {
                        finalString.push('<div id="collapsibleMenu" data-mini="true" data-role="collapsible" data-inset = "true" data-content-theme="g">');
                        finalString.push('<h3>' + row.menu[index].category_name + '</h3>');
                        finalString.push('<ul id="menuDetails" data-role="listview">');
                        for(var j = 0; j < row.menu[index].products.length; j++) {
                            var output = ['<li data-icon="addToCart" id="addToCart"> <a href="javascript:test("+row.menu[index].products[j].prod_id")"><p>' + row.vendor_menu[index].products[j].prod_name + '</p><p> $' + Number(row.vendor_menu[index].products[j].price).toFixed(2) + '</p></a>' + '</li>'];
                            finalString.push(output);
                        }
                        finalString.push('</ul></div>');
                    }
                    $('#output').append(finalString.join(''));
                });
            });
            $('#output').trigger('create');
        }
    });
});

function test(prod_id) {
    alert("entered test " + prod_id);
    addToCart(prod_id, 1);
}

在以下代码中,我正在执行以下操作:

<a href="javascript:test("+row.menu[index].products[j].prod_id")">

这显然给了我一个错误。关键是,我需要将 prod_id 动态传递给 javascript 测试方法。我不知道该怎么做。如果我只是调用 test 而不通过 prod_id,它会很好用。请帮忙!

【问题讨论】:

  • Tsk tsk,生成带有字符串连接的内联事件侦听器的 HTML。你知道,DOM 的存在是有原因的。
  • 这里可能是一个错字,你错过了最后的+:
  • 你写了那么多漂亮的jQuery代码,还懒得写一个简单的点击函数?
  • @KhanhTo 那行不通!!!
  • @adeneo 我可以拥有,但我不知道如何为每一行使用特定的 id。有什么建议吗?

标签: javascript jquery dynamic methods


【解决方案1】:

尝试删除参数中的引号。

我想我可能已经想通了。 试试这个。

<a href="javascript:test(\''+row.menu[index].products[j].prod_id+'\')">

【讨论】:

  • rowindexj 是传递给 $(post).each 的匿名函数的本地函数,不能在内联点击处理程序中访问。
  • 你越来越热了。您只需要在加号前后直接删除双引号即可。
  • @icktoofay 当我这样做时,删除双引号,我得到以下 Uncaught TypeError: Cannot read property '0' of undefined
  • @mustaine:或者任何其他模板引擎!如果 Jade 不符合您的喜好,还有许多其他模板引擎,几乎所有这些引擎都可以简化您的代码。
  • @mustaine 不客气。我同意研究玉。您可能还想尝试将 HTML DOM 与事件一起使用。 DOM 当然更复杂,但也有好处。
【解决方案2】:

这看起来是使用模板引擎的完美理由。您可以使用这样的Jade 模板:

for post in posts
    for row in post
        for menu in row.menu
            #collapsibleMenu(data-mini="true", data-role="collapsible", data-inset="true", data-content-theme="g")
                h3= menu.category_name
                ul#menuDetails(data-role="listview")
                    for product in menu.products
                        li#addToCart(data-icon="addToCart")
                            a(href="#", data-product-id=product.prod_id)
                                p= product.prod_name
                                p= '$' + Number(product.price).toFixed(2)

然后您可以将您的$.ajax 调用简化为:

$.ajax({
    // ...
    dataType: 'json',
    success: function(data) {
        $('#output').append(templateFunction(data));
    }
});

对于点击事件,使用事件委托:

$('#output').on('click', 'a[data-product-id]', function() {
    addToCart(Number($(this).data('product-id')), 1);
});

简单,是吗? 现在将您的所有ids 更改为类,因为ids 必须是唯一的,而您的不是

【讨论】:

  • 这看起来很优雅,但我不知道玉是如何工作的,也不知道如何使用它。有学习曲线吗?我可以对原始代码 sn-p 做些什么来完成这项工作吗?
  • @mustaine:可能有一个学习曲线,但如果你做很多这样的事情,那么它可能会得到回报。是的,您可以对原始代码进行修改以使其正常工作,但它们有点难看,我宁愿将其留给其他回答者之一。
  • @mustaine:我想你可以做的一件事是插入data-product-id 属性而不是href="javascript:..." 的东西,然后使用我拥有的事件委托代码,这不需要 Jade。跨度>
猜你喜欢
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多