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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号