Insert text inside html id after clicking a button

2019-03-06 07:53发布

问题:

I want to include some text inside a textbox (ID=button3) after pressing a button.

I have the following html:

<div id="collapse2" style="display:none">
   <span id="button3">text before pressing the button></span>
</div>

And inside javascript I know I can solve my problem in two different ways.

Option 1:

var element=document.getElementById("button3");
element.innerHTML="text after pressing the button";

Option 2:

$('#button3').text('text after pressing the button');

Can you give me a 3rd option with razor/html helpers? This is related with another question I posted yesterday (My other question). However, I think this time I was able to explain my problem in a better way.

回答1:

To do this entirely within razor, you will be required to make a call to the server. To do this, make your view strongly typed and take the button3 value from the model using razor.

Add an input parameter (string button3Value="initial value") to your Controller Method. Default this to your initial button3 value.

You can then use an @Html.ActionLink and pass the new button3 value from your buttonclick. This will reload the page with the appropriate value.



回答2:

u can use this option

$('#button3').html('text after pressing the button');