I have a textbox where i must to write a value, how to pass this value in my controller if I call my controller using ActionLink, this is my code:
view:
#Html.TextBox("tisch", "", new { #class = "teschChange"})
#Html.ActionLink("Apply Command", "ApplyCommand", "Work")
and this is te script
<script type="text/javascript">
$(function test() {
$(".teschChange").change(function () {
var tisch = $(this).attr('value');
});
});
</script>
I must to send tisch in ApplyCommandController
Thanks
Instead of using an action link, use a standard link:
<a id="l" href="#Url.Action("ApplyCommand", "Work")">Apply Command</a>
Then, you can use this script:
<script type="text/javascript">
$(function test() {
$(".teschChange").change(function () {
var tisch = $(this).attr('value');
$("#l").attr("href", "#Url.Action("ApplyCommand", "Work")/" + tisch);
});
});
</script>
Which will assign a href of: /Work/ApplyCommand/tisch.
this was the correct script:
$("#l").attr("href", "#Url.Action("ApplyCommand", "Work")?tisch=" + tisch);
Related
XSL:
<script type="text/javascript">
<xsl:value-of select="concat('replaceAll(',$idS,');')"/>
</script>
HTML:
<script type="text/javascript">replaceAll(ID0ED);</script>
Javascript:
$(function () {
var cityNameRye = $("#spnCityID0ED").text().split(";")[1];
if (cityNameRye.toLowerCase() == "rye") {
//do something
}
});
I get the following error in the console which causes an issue in the site:
Uncaught ReferenceError: replaceAll is not defined
Can I create a dummy function which clears out the error?
Put this function code somewhere when it could be loaded before those from XSL are executed:
function replaceSpnCity(idPart) {
var idsToReplace = $("[id^='spnCity']"); //updated missing quote
idsToReplace.each(function() {
var id = $(this).attr('id');
var newId = id.replace(idPart,'');
$(this).attr('id',newId);
});
}
And call it from XSL like:
<xsl:value-of select="concat('replaceSpnCity('',$idS,'');')"/>
You do not have any function with the name replaceAll() in your code
try
function replaceAll(varName){
//code to execute
}
and see if you still have the error
I would like to use a jquery odometer to display information on a master page. http://www.jqueryscript.net/animation/Smooth-Animated-Numbers-with-Javascript-CSS3-odometer.html
In order to do that I have to retrieve that value from SQL Server using C#. Then I have to pass it to the jscript odometer in the html() as shown below. If I get the valuje - how do Isend it to the javascript?
<script>
setTimeout(function(){
$('.odometer').html('123222');
}, 1000);
</script>
Try this html
</head>
<body>
<script src="https://rawgit.com/HubSpot/odometer/v0.4.6/odometer.min.js"></script>
<style src="https://rawgit.com/HubSpot/odometer/master/themes/odometer-theme-default.css"></style>
<script>
odometerOptions = {
auto: false
};
</script>
<script type = "text/javascript">
$(function(){
var exampleOdometerValue = 123456;
var exampleOdometer = new Odometer({ el: $('.odometer-example')[0], theme: 'digital', value: exampleOdometerValue });
exampleOdometer.render()
setTimeout(function(){
exampleOdometerValue = exampleOdometerValue+100.07;
exampleOdometer.update(exampleOdometerValue);
}, 2000);
});
</script>
<div class="odometer odometer-example">123</div>
</body></html>
You can use the below implementation to pass the value from code behind to Jquery
This is one example how to do it.
First Declare a Public Variable in code behind
//Declare a Public Variable in code behind
public string odometervalue = "667";
Read the value in Jquery Like given below
<script>
$(function () {
setTimeout(function () {
//Get the value from serverside
var uid = '<%=odometervalue %>';
odometer.innerHTML = uid;
}, 1000);
});
</script>
i want to get the number in text field and send it in Url methode GET ;
$(document).ready(function () {
$("#go").click(function() {
var n = document.getElementById('nombre').value;
alert("Chess.php?number=9");
$('#chess').load("Chess.php?number="+n);
});
the value :
<input type="text" name="nombre" id="nombre">
Try again with code below:
$(document).ready(function () {
$('#go').click(function() {
$.get('Chess.php', { number: $('#nombre').val() } );
});
});
PS: Its stranger the attribute value called nombre and the field called number. Change this too. Other tip is dont use Uppercase for the url, use only 'chess.php'.
I hope to help you.
There's a missing }) ...
Plus you have to add in the head section of your script the following :
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function ()
{
$("#go").click(function()
{
var n = document.getElementById('nombre').value;
alert("Chess.php?number=9");
$('#chess').load("Chess.php?number="+n);
})
});
</script>
it works now like this
$(document).ready(function () {
$("#go").click(function() {
$('#chess').load("Chess.php?number="+document.getElementById('nombre').value);
});
});
on view page I have and inside javascript code I want to access to my Model.Id. How this can be done?
#model MyModel
<script type="text/javascript">
function initialize() {
var id = model.Id // this doesnt work
}
</script>
Thanks
You need to keep track of what's on the server and what's on the client. The #Model variable is only used on the server, Javascript has no notion of the model object, so you need to print the values out in the html.
<script type="text/javascript">
function initialize() {
var id = "#Model.Id"; // this will work
}
</script>
Make model.id to "#Model.id" Like :
#model MyModel
<script type="text/javascript">
function initialize() {
var id = "#Model.Id"
}
</script>
This should work. Still need to use the razor syntax..#.
#model MyModel
<script type="text/javascript">
function initialize() {
var id = "#Model.Id" // this doesnt work
}
</script>
I have an Index.cshtml Page in MVC3 Razor View Engine with Following Code:
#{
ViewBag.Title = "Home Page";
}
<h2>#ViewBag.Message</h2>
<label id="lblClickMe" style="cursor:pointer"> Test Java Script Work in MVC Razor</label> || <label id="lblReset" style="cursor:pointer"> Rest </label>
<br /><br /><br /><br />
<input type="text" id="txtResult" style="width:300px"/>
And i have some JavaScript in Sane page Like :
<script type="text/javascript">
$("#lblClickMe").click(function () {
var txtResukt = document.getElementById("txtResult");
txtResukt.value = "Java script work successfully";
})
$("#lblReset").click(function () {
var txtResukt = document.getElementById("txtResult");
txtResukt.value = "";
})
</script>
When I try to load my view give following exception from "lblClickMe" Click Event :
Microsoft JScript runtime error: Object expected
Any one fix this bug?
Try this:
<script src="#Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script type="text/javascript">
$("#lblClickMe").click(function () {
$("#txtResult").val("Java script work successfully");
});
$("#lblReset").click(function () {
$("#txtResult").val("");
});
</script>
Try this:
$(document).ready(function()
{
$("#lblClickMe").click(function () {
var txtResukt = document.getElementById("txtResult");
txtResukt.value = "Java script work successfully";
})
$("#lblReset").click(function () {
var txtResukt = document.getElementById("txtResult");
txtResukt.value = "";
})
});
This ensures the HTML in the page is loaded first before code attempts to wire up the click events.
Why not do this?
$("#lblClickMe").click(function () {
document.getElementById("txtResult").value = "Java script work successfully";
});
or in pure jQuery:
$("#lblClickMe").click(function () {
$("#txtResult").val("Java script work successfully");
});