Inserting model string in span text after button click

ぃ、小莉子 提交于 2019-12-25 04:25:32

问题


Using razor it's easy to show the values of a list in html:

<div id="collapse1" style="display:none">
@{
    foreach (var i in Model)
    {
        <text> <strong>::: @i.City, @i.Country :::</strong> </text>
        <p style="margin-bottom:0px"></p>

        foreach (var h in i.Weather)
        {
            {
                <div id="MyDiv2">
                    <strong><small>@DateTime.Now.AddDays(count).ToString("dd/MM/yy")</small></strong>
                    <p style="margin-bottom:0px"></p>
                    <text><small>Max: @i.MaxTemp.ElementAt(@count)°</small></text>
                    <p style="margin-bottom:0px"></p>
                    <text><small>Min: @i.MinTemp.ElementAt(@count)°</small></text>
                    <span id="MyTest">the min temp: @i.MinTemp.ElementAt(0) </span>
                </div>
            }
            count++;
        }
    }
}
</div>

Yes, piece of cake. But know imagine that I want to change the text inside the span MyTest after clicking a button so that it has instead: "the max temp: @i.MaxTemp.ElementAt(0)".

My problem is that I would have to mix jQuery (for the button) and razor (to get the maxtemp value). Any help/tip is highly appreciated!


回答1:


<div id="collapse1" style="display:none">
@{
    int mycounter=0;
    foreach (var i in Model)
    {
        <text> <strong>::: @i.City, @i.Country :::</strong> </text>
        <p style="margin-bottom:0px"></p>

        foreach (var h in i.Weather)
        {
            {
                <div id="MyDiv2">
                    <strong><small>@DateTime.Now.AddDays(count).ToString("dd/MM/yy")</small></strong>
                    <p style="margin-bottom:0px"></p>
                    <text><small>Max: @i.MaxTemp.ElementAt(@count)°</small></text>
                    <p style="margin-bottom:0px"></p>
                    <text><small>Min: @i.MinTemp.ElementAt(@count)°</small></text>
                    <span maxtemp=' @i.MaxTemp.ElementAt(0)' id="MyTest">the min temp: @i.MinTemp.ElementAt(0) </span>
                </div>
            }
            count++;
        }
    }
}
</div>
$('#MyTest').on('click',function(){

$(this).text($(this).attr('maxtemp'));

}):


来源:https://stackoverflow.com/questions/21933961/inserting-model-string-in-span-text-after-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!