Reputation: 29514
I'm new to Jquery. How to retrieve the values from my controller and compare them with some String inside JQuery?
$(".select"+increment).change(function ()
{
if("here i need the value from my controller " =='String')
{
$("<label id=labelstr"+stringinc+" >"+labelname+"</label>").appendTo(".menu li");
$("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
}
}
Upvotes: 2
Views: 1014
Reputation: 17548
Your question is very confusing but I will try my best to help. I'm going to assume you want to retrieve this information via AJAX. I'll also assume your CakePHP controller spits out the following when called (you can find out by going to: http://yoursite/Forms/views):
This is some random string
In order to use this as I've presumed, you will need to do this:
<script language="javascript">
$(function() {
$(".select"+increment).change(function () {
$.get('/Forms/views',{},function(data) {
if(data == 'This is some random string') {
// I have no idea where you are getting the
// 'stringinc' and 'labelname' variables from.
$("<label id=labelstr"+stringinc+" >"+labelname+"</label>").appendTo(".menu li");
$("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
}
});
});
});
</script>
Now, if you are trying to do it the way Oliver is suggesting (except just in CakePHP), you would need to do this:
<script language="javascript">
$(function() {
$(".select"+increment).change(function () {
// I'm not familiar with Cake, you might need to use
// some sort of template syntax. Either way, whatever
// method you need to use to get the value into you view
// let's just assume its called '$value_from_controller'.
var data = <?= $value_from_controller; ?>;
if(data == 'This is some random string') {
// I have no idea where you are getting the
// 'stringinc' and 'labelname' variables from.
$("<label id=labelstr"+stringinc+" >"+labelname+"</label>").appendTo(".menu li");
$("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
}
});
});
</script>
I hope that helps in some way.
Upvotes: 0
Reputation: 15268
[edit] : This answer is if by Controller you mean the Controller data passed in the ViewData object of a ASP.NET MVC project. [/edit]
The script will have to be in the aspx/ascx (not in a separate JS file).
<script language="Javascript">
$(".select"+increment).change(function ()
{
if("<%=ViewData["YourData"] %>" =='String')
{
$("<label id=labelstr"+stringinc+" >"+labelname+"</label>").appendTo(".menu li");
$("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
}
}
</script>
Upvotes: 1