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

JQuery问题:ID含字母'X'时输入2功能异常,5皇后求解受阻

Hey there! Let's break down your 5-queen puzzle problem and the jQuery issues you're facing step by step.

1. Fix the Input Value Mismatch Problem

First off, the reason inputting 2 behaves the same as 1 is almost certainly because your code isn't properly reading the latest value from the input box when the button is clicked. Here's how to fix that:

  • Make sure you re-fetch and parse the input value every time the button is clicked (don't rely on a hardcoded initial value). Add this to your button click handler:
    $('#your-button-id').click(function() {
      // Get the input value and convert it to an integer
      const targetUnattacked = parseInt($('#your-input-id').val(), 10);
      // Log it to the console to verify (open F12 > Console to check)
      console.log("Target unattacked cells:", targetUnattacked);
      
      // Now run your queen placement logic using this updated value
      findSolutions(targetUnattacked);
    });
    

This ensures you're always working with the user's latest input, not a stale default value.

2. Fix ID Selector Issues with 'X'

If elements with IDs containing 'X' aren't being selected correctly, here's what to check:

  • Verify your selector syntax: Make sure you're using the # prefix for ID selectors. For example, to select an element with ID X1, use $('#X1') — not $('X1') (that would look for <X1> tags, which don't exist).
  • Check for case sensitivity: jQuery ID selectors are case-sensitive. If your element's ID is X1 but your code uses $('#x1'), it won't match. Double-check the ID strings you generate for your棋盘 cells.
  • Test in dev tools: Open your browser's developer tools (F12), go to the Elements tab, find the element with the 'X' ID, then run the selector in the Console (e.g., $('#X2')) to confirm it selects the right element. If it doesn't, your ID generation code has a typo.

3. Verify Queen Attack Logic

Since your goal is to have exactly 3 unattacked cells on a 5×5 board with 5 queens, make sure your attack range calculation is accurate:

  • A queen attacks all cells in its row, column, and two diagonals.
  • Don't double-count cells attacked by multiple queens. Use a set or a 2D array to track which cells are attacked (mark them once even if multiple queens target them).
  • Remember to exclude the 5 queen positions themselves from the "unattacked" count (since those cells are occupied, not available to be attacked or unattacked).

Quick Check for jQuery Availability

If jQuery is acting weird entirely, confirm it's properly loaded in your environment. In jsfiddle, go to the left sidebar under Frameworks & Extensions and make sure you've selected a jQuery version (e.g., jQuery 3.6.0). If it's set to "No-Library", $ will throw an error.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:10:37