【问题标题】:jQuery search script based on selected option基于所选选项的 jQuery 搜索脚本
【发布时间】:2020-11-02 17:50:14
【问题描述】:

我尝试根据所选选项做简单的搜索方法。

场景:我有一个需要输入姓名的搜索表单。然后需要选择我要搜索的位置并点击提交。它应该打开一个带有以下格式的 URL 的新窗口 - URL - 基于从搜索输入中选择的选项 + 字符串。如果我将所有内容都放在表单值中,它会起作用,但我需要在脚本中设置实际值而不是在 html 值中。

我的 HTML:

<form>
<input id="search" type="search" placeholder="placeholder">
 <select name="selection" id="selection">
  <option class="level_2" value="1">website1.com</option>
  <option class="level_2" value="2">website2.com</option>
  <option class="level_2" value="3">website3.com</option>
</select>
<button id="submit" type="submit">vyhledat</button>
</form>

<script>  
jQuery(document).ready(function( $ ) {
    $('#submit').on('click', function(){
        var searchitem = document.getElementById("search");
        var selecteditem = document.getElementById("selection");
        var selectionitem;
         switch($(selecteditem).val()) {
            case "1": selectionitem = "https://www.wikitree.com/genealogy/";
                 break;
            case "2": selectionitem = "https://www.wikitree.com/genealogy1/";
                 break;
            case "3": selectionitem = "https://www.wikitree.com/genealogy2/";
                 break;
            }
        var url = selectionitem.value + searchitem.value;
        window.open(url,"");
        });
    });
</script>

当我使用“selectionitem”的警报时,它会返回正确的值,但值不会转移到“var url = seletionitem.value”。

感谢您的帮助

【问题讨论】:

  • 只使用了selectionitem 而不是selectionitem.value
  • 另外,要在新标签中打开,您需要:window.open(url, "_blank")
  • @berkobienb 非常感谢它为我节省了很多时间 :) 它有效!
  • 另外,在 JavaScript 中建议使用 camelCase 命名变量

标签: javascript jquery forms search html-select


【解决方案1】:

只需使用selectionitem 而不是selectionitem.value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多