문제

I am trying to change a drop down based on a previously selected drop down. I am able to write the function in jQuery.

Since i am not able to use the URL path across various installation based on the server as i have to hard core the URL into the script.

<script type="text/javascript">
function getCategory(){
    var industryId = $("#CompanyCompanyIndustryId").val();
    $.getJSON('http://localhost/gitgrow/users/fetch/'+industryId, function(data) {
        var options = '';
        $.each(data, function(key, value) { 
          options += '<option value="' + key + '">' + value + '</option>';
        });
        $("#CompanyCompanyCategoryId").html(options);
    })
}
</script>

How to achieve the same using CakePHP and JS component ?

도움이 되었습니까?

해결책

You can write your javascript in the page, and use $this->Html->url to build your URL value. Just write it in $this->Html->scriptBlock(); and add 'inline'=>false to make it write in the head instead of at that point of the document:

$this->Html->scriptBlock("
    function getCategory(){
        var industryId = $(\"#CompanyCompanyIndustryId\").val();
        $.getJSON('" . $this->Html->url(array('controller'=>'users', 'action'=>'fetch', $industryId)) . ", function(data) {
            var options = '';
            $.each(data, function(key, value) { 
              options += '<option value=\"' + key + '\">' + value + '</option>';
            });
            $(\"#CompanyCompanyCategoryId\").html(options);
        })
    }
", array('inline'=>false));

Now you have javascript that's generated in the <head> (or anywhere else you'd like it to be), and are using CakePHP to build the URL.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top