简体   繁体   English

两个提交按钮,一个始终处于隐藏状态,如何按下Enter键并使用正确的提交按钮

[英]Two submit buttons, one is always hidden, how to hit enter and use the correct submit button

BEGINNING OF EDITED*** Updated function, however, hitting "enter" doesn't trigger the second submit button. 开始编辑***更新功能,但是,单击“输入”不会触发第二个提交按钮。 Also adding this line of code: 还添加以下代码行:

document.getElementById('btn-default').removeAttribute("disabled");

in case the user wants to switch back to the original search button instead. 如果用户想切换回原始搜索按钮。 END OF EDIT *** 编辑结束***

I have two submit buttons, the first one is the default for a general input search box. 我有两个提交按钮,第一个是常规输入搜索框的默认按钮。 However, if the user clicks on the "Advance" link it will hide the general search input along with the submit button. 但是,如果用户单击“高级”链接,它将隐藏常规搜索输入以及提交按钮。 And display the "Advance" submit button. 并显示“高级”提交按钮。 When hitting enter it will default to the first submit button. 当按下回车键时,它将默认为第一个提交按钮。 Is there a way to detect when a submit button is hidden, to use the submit button that is displayed? 有没有一种方法可以检测何时隐藏了提交按钮,使用显示的提交按钮? Here is part of my code below: 这是下面我的代码的一部分:

FORM: 形成:

<form id='searchGroup' class='form-inline mt-2 mt-md-0' action='test.php' method='POST'>
    <div class="col-md-5" id="defaultDisplay" >
            <input class='form-control mr-sm-2' type='text' placeholder='' aria-label='Search' name='SearchAll' autofocus='autofocus'>  

            <!-- STANDARD SEARCH BUTTON -->
            <input id='btn-default' class='btn btn-default my-2 my-sm-0' type='submit' name='SearchStd' value='Search'/>
    </div>


    <div class="collapse" id="collapseExample">

            <!-- MULTIPLE INPUTS HERE -->
            <!-- ADVANCED SEARCH BUTTON -->
            <input class='btn btn-primary' type='submit' name='SearchAdv' value='Search'/> 
    </div>
</form>

FUNCTION to display div: 显示div的功能:

    <script>
function switchVisible() {
    if (document.getElementById('defaultDisplay')) {
        if (document.getElementById('defaultDisplay').style.display == 'none') {
            document.getElementById('defaultDisplay').style.display = 'block';
            document.getElementById('btn-default').removeAttribute("disabled");
        }
        else {
            document.getElementById('defaultDisplay').style.display = 'none';
            document.getElementById('btn-default').setAttribute("disabled", "disabled");
        }
    }
}
</script>

One thing you could do is to just create two forms , one for basic search and one for advanced search. 您可以做的一件事就是只创建两种形式 ,一种用于基本搜索,一种用于高级搜索。 Then toggle the display between the two. 然后在两者之间切换显示。 It's a small, negligible redundancy that would fix this issue without resorting to JavaScript workarounds. 这是一个很小的可忽略的冗余,可以解决此问题而无需诉诸JavaScript解决方法。

Alternatively, just use one form for both simple and advanced , and only having one submit button . 或者, 对简单和高级只使用一种形式 ,并且只具有一个提交按钮 Treat your form as an advanced form to begin with. 首先将表格视为高级表格。 A "simple search" would simply be an advanced search with empty advanced fields. “简单搜索”将只是具有空高级字段的高级搜索。

Any input or button on the form with TYPE="submit" will be linked to the ENTER key. 表单上任何带有TYPE =“ submit”的输入或按​​钮都将链接到ENTER键。 Notably in your case, if two such inputs have the TYPE="submit", the first one will be triggered on enter keypress. 值得注意的是,如果您有两个这样的输入都具有TYPE =“ submit”,则第一个输入将在按下Enter键时触发。

One solution could be to set the DISABLED attribute on the one you do not want to trigger on ENTER keypress. 一种解决方案是在您不想在ENTER键上触发的属性上设置DISABLED属性。

According to your requirement, you want the 'Advanced' submit button to be disabled to begin with, so edit your HTML to include the disabled property like this: 根据您的要求,您希望首先禁用“高级”提交按钮,因此,请编辑HTML以使其包含Disabled属性,如下所示:

<input class='btn btn-primary' type='submit' name='SearchAdv' value='Search' disabled="disabled" /> 

Then, when you execute the code to show the 'Advanced' section, add the Disabled attribute to the 'default' section submit button: 然后,当您执行代码以显示“高级”部分时,将“禁用”属性添加到“默认”部分的提交按钮:

document.querySelector('input[name="SearchStd"]').setAttribute('disabled','disabled');

while at the same time removing the DISABLED attribute from the 'advanced' section's button: 同时从“高级”部分的按钮中删除DISABLED属性:

document.querySelector('input[name="SearchAdv"]').removeAttribute('disabled','disabled');

At this stage, I should mention I have noticed that your JavaScript code is working in a slightly different manner to how I understand your app to work: 在此阶段,我应该提到我已经注意到,您的JavaScript代码以与我理解您的应用正常工作的方式稍有不同:

It seems you are saying for each toggle "if the DEFAULT search is display block (shown), set display none (hide it), otherwise if it is not shown, show it" 似乎您对每个切换都说“如果默认搜索是显示块(如图所示),则将显示设置为无(隐藏),否则,如果未显示,则显示它”

I'm pretty sure you planned to be toggling the Advanced section, not the Default section (as the Default is shown by DEFAULT!). 我敢肯定,您计划切换“高级”部分,而不是“默认”部分(因为“默认”由DEFAULT显示!)。 So assuming this is true and assuming you already made the HTML change mentioned above, AND assuming you've made a CSS change so that #collapseExample has display NONE to begin with... you'd want you JS like this: 因此,假设这是正确的,并且假设您已经进行了上述HTML更改,并且假设您已经进行了CSS更改,因此#collapseExample开始显示为NONE,那么您希望JS这样:

<script>
    function switchVisible() {
        if (document.getElementById('collapseExample')) {
            if (document.getElementById('collapseExample').style.display == 'none') {
                document.getElementById('collapseExample').style.display = 'block';
                document.querySelector('input[name="SearchStd"]').setAttribute('disabled','disabled');
                document.querySelector('input[name="SearchAdv"]').removeAttribute('disabled','disabled');
            }
            else {
                document.getElementById('collapseExample').style.display = 'none';
                document.querySelector('input[name="SearchStd"]').removeAttribute('disabled','disabled');
                document.querySelector('input[name="SearchAdv"]').setAttribute('disabled','disabled');
            }
        }
    }
</script>

There are some ways to improve this by the way, for example you could look into "caching elements" so you don't have to repeat the getElementById's, and you could add IDs to the actual inputs which is considered good practice when manipulating unique elements, but to keep it simple and answer your actual question, this is my answer. 有一些方法可以改善这种情况,例如,您可以研究“缓存元素”,因此不必重复getElementById,并且可以在实际输入中添加ID,这在处理唯一元素时被认为是一种很好的做法。 ,但为了简单起见并回答您的实际问题,这是我的答案。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个提交按钮(一个隐藏,一个可见) - Two submit buttons (one hidden and one visible) 如何将Enter键限制为只有两个提交按钮的表单中的一个按钮 - How to limit enter key press to only one button in a form having two submit buttons 我的用户如何按 Enter 键或单击按钮提交并被重定向? - How can my user hit enter or click a button to submit and be redirected? 两个提交按钮,只有一个提交按钮弹出确认信息 - two submit buttons, with confirmation pop up only on one submit button 如何使用键盘上的输入按钮作为提交 - How to use enter button from keyboard as submit 在rails控制器中,如何防止双重提交(当用户双提交按钮或按两次输入时)? - In rails controllers, how to prevent double submit (when user double-clic submit button or hit enter twice)? 如何使用提交按钮和输入按钮提交值? - How to submit a value with a submit button AND by the enter button? 当有两个按钮时,如何使用回车键提交 - How to submit using the enter key when there are two buttons 如何提交单一表格并使用两个提交按钮,一个按钮单击然后验证 - how to submit the single form and use two submit button one button click then validate 按下Enter键后,隐藏的提交按钮将不会以chrome提交 - Hidden submit button wont submit in chrome when enter is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM