Jquery 如何在mvc中获取标记内容
当我点击一个特殊的标签时,我想得到两个标签的内容。当我点击标签a时,如何获得“Lorem产品”和“$23.00”Jquery 如何在mvc中获取标记内容,jquery,asp.net,asp.net-mvc,Jquery,Asp.net,Asp.net Mvc,当我点击一个特殊的标签时,我想得到两个标签的内容。当我点击标签a时,如何获得“Lorem产品”和“$23.00” 谢谢 <div class="item"> <!-- Item's image --> <img class="img-responsive" src="http://lorempixel.com/200/200/food/1/" alt=""> <!-- Item details --> <div
谢谢
<div class="item">
<!-- Item's image -->
<img class="img-responsive" src="http://lorempixel.com/200/200/food/1/" alt="">
<!-- Item details -->
<div class="item-dtls">
<!-- product title -->
<h4><a href="#" >Lorem product</a></h4>
<!-- price -->
<span class="price lblue" >$23.00</span>
</div>
<!-- add to cart btn -->
<div class="ecom bg-lblue">
<a class="btn" href="#" ">Add to cart</a>
</div>
</div>
$23.00
没有jQuery的简单回发示例:
样本模型
public class TestModel
{
public string Name { get; set; }
public int Price { get; set; }
}
控制器动作样本
public ActionResult IndexTest(TestModel model)
{
var tmp = model;
return RedirectToAction("Index");
}
修改了您的视图
@model WebApplication1.Models.TestModel
@using (Html.BeginForm("IndexTest", "Home"))
{
<div class="item">
<!-- Item's image -->
<img class="img-responsive" src="http://lorempixel.com/200/200/food/1/" alt="">
<!-- Item details -->
<div class="item-dtls">
<!-- product title -->
<h4><a href="#">@Html.EditorFor(m => m.Name)</a></h4>
<!-- price -->
<span class="price lblue">@Html.EditorFor(m => m.Price)</span>
</div>
<!-- add to cart btn -->
<div class="ecom bg-lblue">
<button class="btn" type="submit">Add to cart</button>
</div>
</div>
}
将提交嵌套的表单。我建议将其解压缩到
中,以获得更清晰的HTML代码。无需jQuery的简单回发示例:
样本模型
public class TestModel
{
public string Name { get; set; }
public int Price { get; set; }
}
控制器动作样本
public ActionResult IndexTest(TestModel model)
{
var tmp = model;
return RedirectToAction("Index");
}
修改了您的视图
@model WebApplication1.Models.TestModel
@using (Html.BeginForm("IndexTest", "Home"))
{
<div class="item">
<!-- Item's image -->
<img class="img-responsive" src="http://lorempixel.com/200/200/food/1/" alt="">
<!-- Item details -->
<div class="item-dtls">
<!-- product title -->
<h4><a href="#">@Html.EditorFor(m => m.Name)</a></h4>
<!-- price -->
<span class="price lblue">@Html.EditorFor(m => m.Price)</span>
</div>
<!-- add to cart btn -->
<div class="ecom bg-lblue">
<button class="btn" type="submit">Add to cart</button>
</div>
</div>
}
将提交嵌套的表单。我建议将其解压缩到
以获得更清晰的HTML代码。.HTML()
可用于获取任何元素的内容。尝试以下方法:
$('a.btn').click(function () {
var container = $(this).closest('.item');
var title = container.find('.item-dtls > h4 > a').html();
var price = container.find('.price').html();
alert('Title: ' + title + ', Price: ' + price);
});
希望这将对您有所帮助。.html()
可用于获取任何元素的内容。尝试以下方法:
$('a.btn').click(function () {
var container = $(this).closest('.item');
var title = container.find('.item-dtls > h4 > a').html();
var price = container.find('.price').html();
alert('Title: ' + title + ', Price: ' + price);
});
希望这能对您有所帮助。您的问题还不完全清楚。如果要将这些值发布到服务器,则需要有一个表单、输入元素和提交表单的方式(提交按钮)。另一种方法是从jquery进行提交。请编辑您的问题并添加更多详细信息,以了解您面临的问题。另外,你忘了在mvc中包含与mvc(模型和动作)以及im编程相关的代码,我想在点击该标记时将这些值发送到该视图的动作结果。你问什么还不完全清楚。如果要将这些值发布到服务器,则需要有一个表单、输入元素和提交表单的方式(提交按钮)。另一种方法是从jquery进行提交。请编辑您的问题并添加更多详细信息,以了解您面临的问题。另外,你忘了在mvc中包含与mvc(模型和动作)以及im编程实际相关的代码,我想在单击该标记时将这些值发送到此视图的动作结果我想在单击标记a时获得“Lorem product”和“$23.00”,你想每次添加相同的值吗?不,有一个由多个项目组成的视图,当我单击标记a时,我希望获得项目的名称和价格,并通过提交带有锚的表单更新答案。当我单击标记a时,我希望获得“Lorem product”和“$23.00”,您希望每次添加相同的值吗?否,有一个由几个项目组成的视图,当我点击标签a时,我想得到项目的名称和价格,并通过提交带有锚的表单更新答案。