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

如何将id为address的div内容同步至name为localizacion的输入框?

Solution to Sync Div Content to Disabled Input

Hey Kenneth, let's fix that sync issue between your address div and the disabled localizacion input! Here's what's likely going wrong and how to fix it step by step:

Common Issues You Might Be Facing

  1. DOM Loading Timing: If your script runs before the page finishes loading, it can't find the address div or localizacion input elements.
  2. Missing Sync in updateMarkerAddress: Your current function only updates the div's content, but doesn't pass that value to the input.
  3. Selector Mistakes: Double-check that you're targeting the input correctly (name="localizacion" is spelled right and case matches).

Step-by-Step Fix

1. Update the updateMarkerAddress Function

Modify this function to automatically sync the address value to the input every time it updates the div:

function updateMarkerAddress(address) {
  // Get references to both elements
  const addressDiv = document.getElementById('address');
  const locationInput = document.querySelector('input[name="localizacion"]');

  // Update the div's content first
  addressDiv.innerHTML = address;

  // Sync the value to the input (disabled inputs can still have values set via JS)
  if (locationInput) {
    locationInput.value = address;
  }
}

2. Initial Sync on Page Load

If the address div already has content when the page loads, add this script to sync it to the input right away. Make sure it runs after the DOM is ready:

// Wait for the page to fully load before accessing elements
document.addEventListener('DOMContentLoaded', function() {
  const addressDiv = document.getElementById('address');
  const locationInput = document.querySelector('input[name="localizacion"]');

  // Only sync if both elements exist to avoid errors
  if (addressDiv && locationInput) {
    locationInput.value = addressDiv.textContent.trim();
  }
});

3. Quick Sanity Checks

  • Confirm your div has exactly id="address" (no typos or extra spaces)
  • Confirm your input has exactly name="localizacion" (HTML attributes are case-sensitive)
  • Disabled inputs don't block JavaScript from setting their value property, so that's not the root problem.

Why This Works

  • The updated updateMarkerAddress ensures every time the div gets new content, the input is updated immediately.
  • The DOMContentLoaded event guarantees we don't try to interact with elements before they exist on the page.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:42:05