php - 对具有相同 ID 的所有文本字段只读

标签 php javascript

我试图通过仅单击一个按钮来启用/禁用具有相同 ID 的列中文本框的只读属性,但我的代码似乎不起作用。我仍然不熟悉 PHP 的工作原理

PHP 代码:

<input type='text' name='nTotal[$i]' value='$row->Total' size='5'  readonly='true' id='ntotal[$i]'>

JavaScript 代码:

var a = document.getElementById(ntotal); //ntotal[$i] is the ID name of textbox in PHP
for (var i=0;i<a.length;i++){            

 if (a[i].readonly == 'false' ) {
        a[i].readonly = 'true';
         }

 else if(a[i].readonly == 'true'){ 
        a[i].readonly = 'false';
          }
    }
}

谁能告诉我为什么它不起作用?我尝试使用名称而不是 ID,但结果相同。

最佳答案

尝试:

var arr = document.querySelectorAll('[id="'+ntotal+'"]');   
for(var i=0;i<arr.length;i++){
    if(arr[i].getAttribute('readonly')=='readonly')
        arr[i].removeAttribute('readonly');
    else
        arr[i].setAttribute('readonly', 'readonly');
}

关于php - 对具有相同 ID 的所有文本字段只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783318/

相关文章:

php - 查询特定自定义帖子类型时出现问题

php - MySQL 查询仅返回 COUNT

javascript - 正则表达式使链接可点击(仅在 'a href' 而不是 'img src' )

javascript - jquery根据特定位置的特定字符过滤值

PHP MySql 错误无法从数据库读取 id

php - Yii框架如何更新所有记录?

php - 切换所有内容的 SlideToggle 的多个实例

javascript - 模式窗口/背景的问题

javascript - 测试循环内的innerText或textContent

Javascript if 条件与子字符串