如何通过JQuery查找并禁用Lookup按钮及关联输入框
- Incorrect selector for the lookup button: Your
closest(".ew-lookup-list")call isn't finding the right parent element because the hidden input#x_Id_armasteris a sibling of the.ew-lookup-listdiv, not a child of it. That's whybtn.lengthis likely returning 0—your selector isn't matching any elements. - Not properly setting the
disabledstate:btn.attr("disabled")only retrieves the current value of the attribute, it doesn't set it to disable the button. You need to use eitherattr("disabled", true)or (preferably)prop("disabled", true)for boolean properties like disabled.
Here's the corrected code that should work:
// Get the lookup button correctly (sibling of the hidden input) var btn = $("#x_Id_armaster").siblings(".ew-lookup-list").find(".ew-lookup-btn"); // Disable the button btn.prop("disabled", true); // Get the visible lookup text div var lookupText = $("#x_Id_armaster").siblings(".ew-lookup-list").find(".ew-lookup-text"); // Add disabled styling and prevent interaction lookupText.addClass("ew-disabled").css({ "pointer-events": "none", "opacity": 0.65 });
Explanation:
- Selector fix: Using
siblings(".ew-lookup-list")targets the adjacent div that contains both the text area and lookup button, since the hidden input is a sibling of this div. - Disabling the button:
prop("disabled", true)properly sets the button's disabled state, which not only prevents clicks but also applies browser-native disabled styling. - Disabling the text area: Since the visible text is a
<div>(not an input), we add a disabled class and adjust CSS to make it look disabled and prevent any user interaction (pointer-events: nonestops clicks,opacitygrays it out).
If your project already has a predefined disabled class (like ew-disabled which is common in some frameworks), you can use that instead of inline CSS for consistency.
内容的提问来源于stack exchange,提问作者Wilson Teo
相关产品推荐
相关产品推荐

