简体   繁体   English

Dojo等效于以下jquery代码

[英]Dojo equivalent of the following jquery code

I am working on a project that I wanted to use jquery for, however it's come to light that it is mandatory to write in dojo , I've never worked with it before and am having some trouble reading up online as to how to use it. 我正在开发一个我想使用jquery的项目,但是事实证明必须使用dojo编写代码,我以前从未使用过它,并且在在线阅读如何使用它时遇到了一些麻烦。

Below is the code I was using: 以下是我使用的代码:

<script>
        window.onload = function() {
            getManager($("#team_name").val());  
        }

        function getManager(team) {
            $.ajax({
                type: "POST",
                url: "getManager.php",
                data: {team:team}
            }).done(function( manager ) {
                $("#manager_name").val(manager);
            });
        }
</script>

Essentially, there is a drop down <selection> field, when selected (and upon window loading) it should prefill the read-only box below it based upon which team is listed. 本质上,有一个下拉<selection>字段,当选中(并在加载窗口时)时,它应根据列出的团队填充其下方的只读框。 See the below screenshot for a better idea: 有关更好的主意,请参见以下屏幕截图:

屏幕截图

I the above code works fine with jquery, I can't seem to find the equivalent for dojo . 我上面的代码与jquery一起正常工作,我似乎找不到dojo的等效项。

This is the html for the selection field: 这是选择字段的html:

<select name="team" id="team_name" onchange="getManager(this.value)" type="text" readonly>

and this is the html code for text input field : 这是用于文本输入字段的html代码:

<input name="manager_name" id="manager_name" type="text" readonly/>

The code must be written in dojo as the rest of the page does, unless there is a way to override the dojo with jquery? 代码必须像页面其余部分一样用dojo编写 ,除非有一种方法可以用jquery覆盖dojo?

window.onload = function() {
    getManager(document.getElementById('team_name').value);  
}

function getManager(team) {
    require(["dojo/_base/xhr"], function(xhr){
        xhr.post({
            url:"getManager.php",
            timeout: 4000,
            content: { team:team },
            load: function( manager ){
                document.getElementById("#manager_name").value = manager;
            }
        });
    });
}

If you are using dojo 1.7 and above no need to use window.onload() or even dojo.addOnLoad() - I think this is what you are looking for 如果您使用的是dojo 1.7及更高版本,则无需使用window.onload()或什至dojo.addOnLoad()-我认为这就是您要寻找的

require(['dojo/dom', 'dojo/domReady!', function(dom) {
  getManager(dom.byId('team_name').value); 
});


function getManager(team) {
  require(['dojo/request/xhr', 'dojo/dom'], function(xhr, dom) {
    xhr.post('getManager.php', {
      method: 'POST',
      data: {team: team}
    }).then(function(manager) {
        dom.byId('manager_name').value = manager;
    }, function(error) {
        console.error('couldn\'t fetch manger!');
    });
  });
}

domReady! domReady! - is an AMD loaded plugin that will wait until the DOM has finished loading before returning -是AMD加载的插件,将等待DOM完成加载后再返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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