在 ColdFusion 中通过 URL 传递后,如何保留在下拉菜单中所做的选择?

发布于 2024-10-16 08:59:46 字数 231 浏览 1 评论 0原文

我的 ColdFusion 页面上有一个下拉菜单,可以根据选择过滤数据列表。通过 URL 传递选择并显示结果。问题是我想保留页面重新加载后所做的选择。现在每次都会回到第一个选项。

这是我通过下拉菜单的 onChange 来通过 URL 传递它的方法:

<cfif IsDefined(url.filterBy)>
     query...
</cfif>

I have a drop down menu on my ColdFusion page that filters a list of data based on its selection. The selection is passed through the URL and the results are displayed. The problem is that I would like to keep the selection that was made after the page has reloaded. Right now it goes back to the first option every time.

This is how I pass it through the URL by the onChange of the drop down menu:

<cfif IsDefined(url.filterBy)>
     query...
</cfif>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与之呼应 2024-10-23 08:59:46

在选项标记内,放置一个 if 语句来检查通过表单范围传递的值。

所以:

<select name="myDropdown" id="myDropdown">
  <option value="someValue"<cfif Form.myDropdown EQ 'someValue'> selected="selected"</cfif>>Some value</option>
</select>

Inside your option tag, you place an if statement that checks the value passed via the form scope.

So:

<select name="myDropdown" id="myDropdown">
  <option value="someValue"<cfif Form.myDropdown EQ 'someValue'> selected="selected"</cfif>>Some value</option>
</select>
绮筵 2024-10-23 08:59:46

如果我正确地阅读了您的问题,您只需查看现有的 URL.filterby 值是否与您的 SELECT 中的值匹配。

<cfparam name="URL.filterby" value="" />
...
<select name="mySelect" onChange="window.location.href='myPage.cfm?filterBy=' + this.value">
  <option value="option1" #selectIf(URL.filterby IS "option1")#>Option 1</option>
  <option value="option2" #selectIf(URL.filterby IS "option2")#>Option 2</option>
</select>

CFPARAM 确保 URL.filterby 始终有一个值。 selectIf 只是一个 UDF,我用它来防止 CFML 代码弄乱我的 HTML。它只接受任何可以解释为布尔值的内容并返回适当的字符串。

function selectIf(checkVal) {
  if (checkVal) {
    return "selected=""selected""";
  } else {
    return "";
  }
}

然后页面的其余部分可以查看 URL.filterby,如果它有值,则执行查询并显示数据。

<cfif structKeyExists(URL,"filterby") AND URL.filterby IS NOT "">
  <!--- query here, etc. --->
  ...

If I'm reading your question right, you just need to see if the existing URL.filterby value matches a value in your SELECT.

<cfparam name="URL.filterby" value="" />
...
<select name="mySelect" onChange="window.location.href='myPage.cfm?filterBy=' + this.value">
  <option value="option1" #selectIf(URL.filterby IS "option1")#>Option 1</option>
  <option value="option2" #selectIf(URL.filterby IS "option2")#>Option 2</option>
</select>

The CFPARAM ensures that there's always a value for URL.filterby. selectIf is just a UDF that I use to keep from cluttering my HTML with CFML code. It simply takes anything that can be interpreted as a boolean and returns a string as appropriate.

function selectIf(checkVal) {
  if (checkVal) {
    return "selected=""selected""";
  } else {
    return "";
  }
}

Then the rest of your page can look at URL.filterby and if it has a value, then do your query and display your data.

<cfif structKeyExists(URL,"filterby") AND URL.filterby IS NOT "">
  <!--- query here, etc. --->
  ...
二货你真萌 2024-10-23 08:59:46

所以,我想出了一种不同的方式(某种程度上)。我所做的就是抓取 URL 的字符串查询部分,仅修剪国家/地区名称并将其与下拉菜单的值进行比较。

<option value="#CountryName" 
  <cfif #CountryName# EQ #replace(#CGI.StringQuery#,"filterBy=","")#>
    selected="selected"
  </cfif>>#CountryName#
</option>

在大多数情况下,效果很好。当涉及到包含多个单词的国家/地区名称时,空格会将其搞乱并且无法保留选择。我也尝试替换空格:

#replace(#CGI.StringQuery#,"%20"," ")#

但是,没有好处。有人有解决方案来识别空间吗?

So, I've figured it out a different way (sort of). What I do is grab the String Query part of the URL, trim just the Country Name and compare it the values of the drop down menu.

<option value="#CountryName" 
  <cfif #CountryName# EQ #replace(#CGI.StringQuery#,"filterBy=","")#>
    selected="selected"
  </cfif>>#CountryName#
</option>

Works good, for the most part. When it comes down to country names with more than one word, the spaces mess it up and it doesn't keep the selection. I tried also replacing the spaces as such:

#replace(#CGI.StringQuery#,"%20"," ")#

But, no good. Anybody have a solution for this to recognize the spaces?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文