求JavaScript函数:将ASCX动态表格中只读文本框设为可编辑
Got it, let's tackle this problem step by step. Here's how you can implement the JavaScript function to make those read-only textboxes editable in your dynamic ASCX table:
Solution Implementation
1. Core JavaScript Function
This function will target the specific textboxes generated by your clsHtmlInputCurr control, remove their read-only restriction, and ensure the required event handlers work as expected:
function makeAssetUnitPriceEditable() { // Target textboxes using the name prefix from your C# code const targetTextBoxes = document.querySelectorAll(`input[name^='${cCTRNAMEPREFIX_ASSET_UNITPRICE}']`); targetTextBoxes.forEach(textBox => { // Remove readonly attribute to enable editing textBox.removeAttribute('readonly'); // Re-bind the event handlers defined in your C# code // (In case readonly state prevented event triggering) textBox.addEventListener('click', () => startEdit(textBox)); textBox.addEventListener('input', () => calcAssets(textBox)); // Optional: Add visual feedback to indicate editable state textBox.style.borderColor = '#66afe9'; textBox.style.backgroundColor = '#f8f9fa'; }); }
2. Key Details Explained
- Targeting Controls: We use
document.querySelectorAllwith an attribute selector (name^='prefix') to precisely pick only the textboxes tied to your asset unit price field, avoiding unintended changes to other inputs. - Removing Read-Only State:
removeAttribute('readonly')directly clears the restriction imposed by yourNot IsEditable(...)logic in C#. - Re-binding Events: Read-only inputs often don't trigger user interaction events, so we reattach the
startEditandcalcAssetshandlers you defined in your server-side code to ensure functionality works once editable. - Visual Feedback: The optional style changes help users immediately recognize that the field is now editable.
3. How to Trigger the Function
You can call this function in response to a user action (like an "Edit" button click) or on page load if needed:
Example: Button Trigger
<button type="button" onclick="makeAssetUnitPriceEditable()">Enable Unit Price Editing</button>
Example: On Page Load (If Default Editable State Is Needed)
document.addEventListener('DOMContentLoaded', () => { // Uncomment below to make fields editable on page load // makeAssetUnitPriceEditable(); });
4. Notes for Your C# Context
- Ensure your
clsHtmlInputCurrcontrol sets thenameattribute correctly using thecCTRNAMEPREFIX_ASSET_UNITPRICEprefix—this is critical for the JavaScript selector to work. - If your server-side logic uses
disabledinstead ofreadonlyfor restricted fields, replaceremoveAttribute('readonly')withremoveAttribute('disabled')in the JS function.
内容的提问来源于stack exchange,提问作者Kartik Choumal
相关产品推荐
相关产品推荐

