donljt2606 2015-12-31 18:52
浏览 23
已采纳

单击“链接”以选择值并提交表单

I have an existing form using drop down menu with submit button and it works fine.

I would like to add direct links for select categories links or images outside of the form - how can I accomplish this?

My attempt below does not work.
Any suggestions are appreciated!

<form method="post" action="page.php?cat" id="searchcat"> 
    <select name="Categories">
        <option selected>Browse...</option>
        <option value="j001">Option 1</option>
        <option value="j002">Option 2</option>
        <option value="j003">3</option>
        <option value="j004">4</option>
        ...
    </select>
</form>

<a onclick="SelectItemByValue(document.getElementById('searchcat'), j004);">
    Click here to submit option#4
</a>

<script>
function SelectItemByValue(element, value) {
    if (value !== undefined && value !== null) {
        var length = element.options.length;
        for (var i = 0; i < length; i++) {
            if (element.options[i].value === value) {
                element.selectedIndex = i;
                return;
            }
        }
    }
}
</script>
  • 写回答

1条回答 默认 最新

  • dqce48404 2015-12-31 19:19
    关注

    Straightforward, take a look at the lines with changed, both in html and javascript:

    <form method="post" action="page.php?cat" id="searchcat"> 
        <select name="Categories" id='cats'>  <!-- changed -->
            <option selected>Browse...</option>
            <option value="j001">Option 1</option>
            <option value="j002">Option 2</option>
            <option value="j003">3</option>
            <option value="j004">4</option>
            ...
        </select>
    </form>
    
    <a onclick="SelectItemByValue(document.getElementById('searchcat'), 'j004');"> <!-- changed -->
        Click here to submit option#4
    </a>
    
    <script>
    function SelectItemByValue(form, value) { // changed
        var sel = form.elements.namedItem('cats'); // changed
        if (value !== undefined && value !== null) {
            var length = sel.options.length; // changed
            for (var i = 0; i < length; i++) {
                if (sel.options[i].value === value) { // changed
                    sel.selectedIndex = i;
                    form.submit(); // changed
                    return;
                }
            }
        }
    }
    </script>
    


    Your code had some problems, such as:
    • You were missing ' on j004, so: 'j004';
    • Your element variable didn't refer to the <select> element; and
    • You were missing the actual call to submit the form.


    You mentioned that you have a button, like so:
    <input id="search2" type="submit" name="submit" value="Search">
    

    But in my tests, naming a button as submit caused the script to stop working. This code worked for me:

    <button name='sent'>Search</button>
    

    But be advised in case you are using the button as isset: If the form is sent via the anchor, there will be no sent to check if isset...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图