You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求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.querySelectorAll with 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 your Not IsEditable(...) logic in C#.
  • Re-binding Events: Read-only inputs often don't trigger user interaction events, so we reattach the startEdit and calcAssets handlers 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 clsHtmlInputCurr control sets the name attribute correctly using the cCTRNAMEPREFIX_ASSET_UNITPRICE prefix—this is critical for the JavaScript selector to work.
  • If your server-side logic uses disabled instead of readonly for restricted fields, replace removeAttribute('readonly') with removeAttribute('disabled') in the JS function.

内容的提问来源于stack exchange,提问作者Kartik Choumal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:39:59