タグごとに4つの<div>
タグとタグがあります。すべてのタグに、2 つのタグと 1 つのタグを挿入しました。<a>
<div>
div
span
a
タグがクリックされたらa
、製品名とその価格を取得する必要がありますdiv
ここにデモhttp://jsfiddle.net/8VCWU/があります
回答のコードを使用すると、以下の警告メッセージが表示されます...
タグごとに4つの<div>
タグとタグがあります。すべてのタグに、2 つのタグと 1 つのタグを挿入しました。<a>
<div>
div
span
a
タグがクリックされたらa
、製品名とその価格を取得する必要がありますdiv
ここにデモhttp://jsfiddle.net/8VCWU/があります
回答のコードを使用すると、以下の警告メッセージが表示されます...
$(".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 関数のパラメータとして使用したり、スタイル シートで要素を参照したりできます。
これを試して:
$(".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
代わりにクラスを使用するように、要素とその子を変換しました。
解決策は以下です
打撃コードを使用して試してください
<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;
}
}
jQueryを使用
$('a.get').on('click',function(){
var parent = $(this).parent();
var name = $(parent+' #postname').text();
var price = $(parent+' #price').text();
});
またはもう一度:
$('a').click(function(e){
e.preventDefault();
var $price = $(this).siblings('#price').text();
var $postname = $(this).siblings('#postname').text();
alert($price);
alert($postname);
});
コードに複数ある場合は、id の代わりに classed を使用することをお勧めします。探している関数は siblings() http://api.jquery.com/siblings/です
更新されたフィドルは次のとおりです: http://jsfiddle.net/8VCWU/14/
試す
function getPrice(currentClickObject)
{
var priceSpan = $(currentClickObject).parent("div:first").children("#price");
alert($(priceSpan).html());
}
そしてあなたのタグに追加してください:
<a href="..." onClick="getPrice(this)">...</a>
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つずつコメントを外すことで、上記のすべての選択肢を確認できます。すべて正常に動作しています。
こんにちは、同じ ID を複数回使用することは問題です。
jQuery と私が提供したマークアップを使用すると、解決策は簡単です。
以下のフィドルの CSS をメモします。
$(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);
});
});