javascript - 将所有 UI 表单设置为只读/禁用?

标签 javascript html dojo dijit.form

我们有一个要求,当用户访问权限设置为“只读”时,我们需要将所有表单输入元素设置为只读

我们在编码中使用基于小部件的模板方法,其中小部件在模板 HTML 中指定,并在相应的 JS 文件中使用。

我在我的 JS postcreate 方法中尝试了以下代码:

var item=dojo.query("#showtech_log_title_pane"); // id of the content pane

for (var i=0;i{
   dijit.byId(item[i]).set('readOnly',true);
}

Error : dijit by id-> undefined or null

还有,

var container = dojo.query("#showtech_log_title_pane");

dojo.query('input', container).forEach(
  function(inputElem){
    inputElem.disabled = 'disabled';
  }
)

Error: Create Element not a method

最佳答案

要禁用小部件,您必须获取对它的引用,并将其禁用属性设置为 true。

在您的情况下,您必须遍历所有具有 ID 的输入(以确保这是一个已创建的小部件),然后获取输入的封闭小部件并将其设置为禁用

下面是一个工作示例:

require([ "dojo/ready", 
		  "dojo/query",
          "dijit/registry",
          "dojo/_base/array",
		  "dijit/TitlePane", 
		  "dojo/domReady!"],
function (ready, query, registry, array, TitlePane) {
	ready(function(){
      //get all 
      var inputs = dojo.query('input', "enclosing_input_div");
      array.forEach(inputs,function(input){
        //check if a it's a created widget input (all widgets have theire own id )
      	if(input.id)
          if(registry.byId(input.id)) // recheck if it it has enclosing widget
            registry.byId(input.id).set("disabled",true)
      })
    });
});
<link href="https://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet"/>
<script>
    dojoConfig= {
        parseOnLoad: true,
        async: true
    };
</script>
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>

<body class="claro">
  <div id="enclosing_input_div">
    <div id="tp1" data-dojo-type="dijit/TitlePane" data-dojo-props="title: 'tp1'">
      <table>
        <tr>
          <td>TextField :</td>
          <td><input  data-dojo-type="dijit/form/TextBox"></td>
        </tr>
        <tr>
          <td>NumberTextBox :</td>
          <td><input id="numberTextBox" data-dojo-type="dijit/form/NumberTextBox"></td>
        </tr>
        <tr>
          <td>Checkbox :</td>
          <td><input id="checkBox" data-dojo-type="dijit/form/CheckBox"></td>
        </tr>
        <tr>
          <td>RadioButton :</td>
          <td><input id="radioButton" data-dojo-type="dijit/form/RadioButton"></td>
        </tr>
      </table>
    </div>
  </div>
</body>

Here is also a fiddle

关于javascript - 将所有 UI 表单设置为只读/禁用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41562064/

相关文章:

javascript - 如何获取 click() 函数内元素的值?

javascript - 如何为html类添加事件监听器?

Javascript - 在异步函数中使用多个 try/catch 的最佳实践

javascript - 如何在 dojo 中获取选中的组合框

javascript - 尝试使用 jQuery 的 get 方法填充 html 选项列表

javascript - 如何更改键盘输入并在输入字段中显示更改的数据

php - 无法实现随机背景图片

html - 根据不是父级的另一个 div 设置 div 的边距

dojo/form/select onchange 事件在 dojo 1.8 中对我不起作用

javascript - 如何将 JsonRestStore 与我的 RESTful 服务结合使用