简体   繁体   中英

Changing graph on radio button selection change - ASP.NET MVC

I use Microsoft chart and I have below line of code to get chart.

 <img src="@Url.Action("GetChart")" alt="Bar Chart using MVC" class="img-responsive" />

I have a radio button list and upon changing the selection, i need to show a different chart in the same place.

<form method="post" id="frmFormType">
        @Html.Label("Select Chart Type")
        @Html.RadioButton("rbChartType", "1", isChecked: true) @Html.Label("First Option")
        @Html.RadioButton("rbChartType", "2", isChecked: false) @Html.Label("Second option")
    </form>

I'm not sure if I can use jQuery change function on this "rbChartType" or use something take advantage of Form. I don't really need to re-post the page but only to change graph. I am not sure how to refetch new chart when selection changed using jQuery. Can anyone please throw some light on this? Thanks

Have both the images:

Make them this way:

 $(function () { $("input").click(function () { $(".graph").addClass("hidden"); $("#" + $(this).attr("value")).removeClass("hidden"); }); }); 
 img {display: block;} .hidden {display: none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <label><input type="radio" name="graph" value="graph-1" /> Graph 1</label> <label><input type="radio" name="graph" value="graph-2" /> Graph 2</label> <img src="https://placeholdit.imgix.net/~text?txtsize=50&txt=Graph%201&w=200&h=200" class="graph hidden" id="graph-1" /> <img src="https://placeholdit.imgix.net/~text?txtsize=50&txt=Graph%202&w=200&h=200" class="graph hidden" id="graph-2" /> 

you can try it!

$(function(){
   $(":radio").click(function() { 
       //do something 
    }); 
})

if above soluation did not work,please use the debug of browser to look for the correct documents. in this way ,you can operate documents with jquery.


First,you have to get the srcs of each RadioButton ,if you want to change the img of src by click radio. $(function(){ $("input:radio").click(function(){ if($(this).is("#1")){$(".img").css('background',src1);}else{$(".img").css('background',src2);}})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM