在 jquery 中,我可以将只读属性添加到我唯一可以触摸的 属性 是函数的输入吗?

in jquery can i add a readonly attribute to a input that the only property i can touch is a function?

我有多个这样的输入(未知数量)

<INPUT id="ctl00_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return scriptReadonly();" style="WIDTH: 150px" name=ctl00$ContentPlaceHolder$txtSOPurchaserSapID>

不在应用程序的同一页面上,id 和名称是动态加载的我无法添加 class 因为有太多而且我不知道所有这些输入的位置.

我的问题是,我可以将 readonly 和 jquery 的属性附加到此输入上吗? onkeydown="return scriptReadonly();

是的,你可以,使用 attribute equals selector:

$('input[onkeydown="return scriptReadonly();"]').prop("readonly", true);

实例:

function scriptReadonly() { }
$('input[onkeydown="return scriptReadonly();"]').prop("readonly", true);
<INPUT id="ctl00_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return scriptReadonly();" style="WIDTH: 150px" name=ctl00$ContentPlaceHolder$txtSOPurchaserSapID>
<INPUT id="ctl01_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return scriptReadonly();" style="WIDTH: 150px" name=ctl01$ContentPlaceHolder$txtSOPurchaserSapID>
<INPUT id="ctl02_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return scriptReadonly();" style="WIDTH: 150px" name=ctl02$ContentPlaceHolder$txtSOPurchaserSapID>
<INPUT id="ctl03_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return scriptReadonly();" style="WIDTH: 150px" name=ctl03$ContentPlaceHolder$txtSOPurchaserSapID>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

如果 onkeydown 属性的文本有点不同,您可以使用 filter 来使事情更加模糊:

$('input').filter(function() {
  return $(this).attr("onkeydown").indexOf("scriptReadonly") != -1;
}).prop("readonly", true);

实例:

function scriptReadonly() { }
$('input').filter(function() {
  return $(this).attr("onkeydown").indexOf("scriptReadonly") != -1;
}).prop("readonly", true);
<INPUT id="ctl00_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return /*0*/scriptReadonly();" style="WIDTH: 150px" name=ctl00$ContentPlaceHolder$txtSOPurchaserSapID>
<INPUT id="ctl01_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return /*1*/scriptReadonly();" style="WIDTH: 150px" name=ctl01$ContentPlaceHolder$txtSOPurchaserSapID>
<INPUT id="ctl02_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return /*2*/scriptReadonly();" style="WIDTH: 150px" name=ctl02$ContentPlaceHolder$txtSOPurchaserSapID>
<INPUT id="ctl03_ContentPlaceHolder_txtSOPurchaserSapID" onkeydown="return /*3*/scriptReadonly();" style="WIDTH: 150px" name=ctl03$ContentPlaceHolder$txtSOPurchaserSapID>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>