1

タグごとに4つの<div>タグとタグがあります。すべてのタグに、2 つのタグと 1 つのタグを挿入しました。<a><div>divspana

タグがクリックされたらa、製品名とその価格を取得する必要がありますdiv

ここにデモhttp://jsfiddle.net/8VCWU/があります

回答のコードを使用すると、以下の警告メッセージが表示されます... 警告メッセージ

4

9 に答える 9

1

jQuery

$(".get").click(function(event){    

    event.preventDefault(); /*To Prevent the anchors to take the browser to a new URL */

    var item = $(this).parent().find('#postname').text();
    var price = $(this).parent().find('#price').text(); 
    var result = item + " " + price;    
    alert(result)
});

デモ

idに関する簡単なメモ:

id 属性は、HTML 要素の一意の ID を指定します (値は HTML ドキュメント内で一意である必要があります)。

要素を識別するための一意の識別子。これを getElementById() やその他の DOM 関数のパラメータとして使用したり、スタイル シートで要素を参照したりできます。

于 2012-08-03T08:55:24.527 に答える
1

これを試して:

$(".get").click(function(e) {
    e.preventDefault();
    var $parent = $(this).closest(".item");
    var itemName = $(".postname", $parent).text();
    var itemPrice = $(".price", $parent).text();

    alert(itemName + " / " + itemPrice);    
});

フィドルの例

id無効なコードであり、問​​題の原因となる多くの繰り返し属性があることに注意してください。#item代わりにクラスを使用するように、要素とその子を変換しました。

于 2012-08-03T08:48:10.993 に答える
1

解決策は以下です

打撃コードを使用して試してください

<a data-role="link" href="javascript:linkHandler('<%= obj.productname %>', '<%= obj.price %>')" class="get" >Add <a>



function linkHandler(name, price)
{
    alert(name);
    alert(price);
    var name = name;
    var price = price;
    var cartItem = new item(name, parseFloat(price));
            // check duplicate
            var match = ko.utils.arrayFirst(viewModel.cartItems(), function(item){ return item.name == name; });
            if(match){
                match.qty(match.qty() + 1);
            } else {
              viewModel.cartItems.push(cartItem);
var rowCount = document.getElementById("cartcontent1").getElementsByTagName("TR").length;
document.getElementById("Totala").innerHTML = rowCount;
            }
}
于 2012-08-03T10:05:56.050 に答える
0

jQueryを使用

​$('a.get').on('click',function(){
 var parent = $(this).parent(); 
 var name = $(parent+' #postname').text(); 
 var price = $(parent+' #price').text();    
});​​​​​​​​
于 2012-08-03T08:50:59.847 に答える
0

またはもう一度:

$('a').click(function(e){

    e.preventDefault();
    var $price = $(this).siblings('#price').text();
    var $postname = $(this).siblings('#postname').text();
    alert($price);
    alert($postname);

});
于 2012-08-03T08:51:25.053 に答える
0

コードに複数ある場合は、id の代わりに classed を使用することをお勧めします。探している関数は siblings() http://api.jquery.com/siblings/です

更新されたフィドルは次のとおりです: http://jsfiddle.net/8VCWU/14/

于 2012-08-03T08:56:20.520 に答える
0

試す

function getPrice(currentClickObject)
{
   var priceSpan = $(currentClickObject).parent("div:first").children("#price");
   alert($(priceSpan).html());
}

そしてあなたのタグに追加してください:

<a href="..." onClick="getPrice(this)">...</a>
于 2012-08-03T08:54:57.490 に答える
0

HTMLタグにいくつかの変更を加え、繰り返されるすべてのIDをクラスに置き換えました。これは、HTMLで多くのIDが繰り返され、問題が発生して構造が間違っているためです。HTML では、すべてのタグに一意の ID を指定する必要があります。他のタグと競合することはありません。

ここで、完全なビンのデモを行いました。また、適切な jQuery セレクターを使用してタグ コンテンツを検索するすべての代替方法も指定しました。デモのリンクは次のとおりです。

デモ: http://codebins.com/bin/4ldqp8v

jQuery

$(function() {
    $("a.get").click(function() {

        var itemName = $(this).parent().find(".postname").text().trim();
        var itemPrice = $(this).parent().find(".price").text().trim();

        //OR another Alternate
        // var itemName=$(this).parents(".item").find(".postname").text().trim();
        // var itemPrice=$(this).parents(".item").find(".price").text().trim();

        //OR another Alternate

        //var itemName=$(this).closest(".item").find(".postname").text().trim();
        //  var itemPrice=$(this).closest(".item").find(".price").text().trim();

        //OR another Alternate

        //var itemName=$(this).siblings(".postname").text().trim();
        //var itemPrice=$(this).siblings(".price").text().trim();
        alert(itemName + " / " + itemPrice);

    });
});

デモ: http://codebins.com/bin/4ldqp8v

1つずつコメントを外すことで、上記のすべての選択肢を確認できます。すべて正常に動作しています。

于 2012-08-03T13:10:43.493 に答える
0

こんにちは、同じ ID を複数回使用することは問題です。

jQuery と私が提供したマークアップを使用すると、解決策は簡単です。

以下のフィドルの CSS をメモします。

http://jsfiddle.net/8VCWU/27/

$(document).ready(function(){
    $("#itmLst a.get").click(function(){
        var $lstItm = $(this).parents("li:first");
        var pName = $lstItm.find("span.postname").html();
        var price = $lstItm.find("span.price").html();

        alert("Product Name: " + pName + " ; Price: " + price);
    });
});
于 2012-08-03T09:18:52.767 に答える