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

如何将两个下拉列表的jQuery change事件函数合并为一个?

Merging Your Dropdown Change Handlers

You can easily combine these two change event listeners into a single function that handles both AJAX loads when either dropdown changes. Here's how:

$('#Ctry,#Yd').change(function () {
    // Cache the dropdown values once to avoid repeated DOM lookups
    const ctryValue = $('#Ctry').val();
    const ydValue = $('#Yd').val();

    // Load content into #Rial from the "tal" action
    const talUrl = '@Url.Action("tal", "St")';
    $('#Rial').load(talUrl, { ctry: ctryValue, yd: ydValue });

    // Load content into #con from the "tala" action
    const talaUrl = '@Url.Action("tala", "St")';
    $('#con').load(talaUrl, { ctry: ctryValue, yd: ydValue });
});

Key Notes:

  • We only attach one change event listener to the #Ctry and #Yd elements instead of two, keeping your code cleaner and avoiding redundant event handling.
  • Caching the dropdown values (ctryValue and ydValue) reduces unnecessary DOM queries, making the code slightly more efficient.
  • Both AJAX load() calls still run whenever either dropdown is updated—they’re just grouped together in a single handler now, maintaining the original functionality.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:45:53