I just spent ages trying to get Blazor not to submit and reload the page but fire off my methods instead on enter key. With
<form>
<input type="text" class="form-text" #bind-value:event="oninput" #onkeydown="Enter" #bind-value="#searchString" />
<button type="button" class="btn btn-primary" #onclick="Search">Search</button>
</form>
Whenever I hit enter the page would get reloaded if I used the enter key instead of click, preventing the results from showing. So I added
<button type="submit" disabled hidden></button>
Which fixed it.
Now this to me looks like a workaround. Is there a more elegant way to do this? If I had the original button as submit it wouldnt work either. I think this works because there is a submit button for the enter key to hit, but being disabled it cant do anything.
Using the form tag is wrong in most cases under Blazor, anyway you can find a good example of using conditionally keydown here:
https://www.syncfusion.com/faq/blazor/forms-and-validation/how-do-i-conditionally-preventdefault-inside-the-input-component-in-blazor
I'm working on a web application which has multiple screens, every screen has 2 submit buttons(1 is the Back button and the other is next button) like below.
<input name="submit" class="order-2 btn btn-primary" type="submit" value="Next" disabled>
<input name="back" class="order-1 btn btn-outline-secondary mr-2" type="submit" value="Back"
formnovalidate formaction="BackToIndex?tabId=#Model.TabId">
The "Next" button is enabled only when all the required fields are filled up in the form, so when I click on GO button when the Next button is disabled it is redirecting me to previous page(which means Back button action is hitting)...
But it should not do that instead it should be in the same page when the "Next" is disabled.
How can I achieve this??
I'm running into a strange issue where Internet Explorer is adding an additional query string parameter that no other browser adds.
The page has a form with auto-submit functionality and a "Reset Filters" button. When a user hits the enter key, it forces the submit. When a user hits enter in Internet Explorer, for some reason the "Reset Filters" operation is selected rather than the submit button.
For example, IE adds this to the query string:
?search=this+is+text&op=Reset+Filters
In all other browsers the same query looks like this:
?search=this+is+text
When I check the $_GET superglobal in PHP, I noticed that op is only being added when I run the application in IE and only when I hit the enter key in the form.
Based on the HTML below, it kind of makes sense that hitting enter would add op to the query string because both the submit button and the reset button are contained within the form. But why would op only get added to IE?
<form>
...
<div class="submit-button">
<input class="form-submit" type="submit" id="edit-submit-fda-views" name="" value="Submit">
</div>
<div class="reset-button">
<input type="submit" id="edit-reset" name="op" value="Reset Filters" class="form-submit">
</div>
...
</form>
Any idea why this might be happening?
UPDATE: One other piece of information that might be important. Because the form is auto-submit, the first submit button is actually hidden. I'm wondering if that's why IE is using the second button as the submit handler.
After doing some more research I realized I asked the wrong question. However, it's not letting me delete the question, so I'm posting the answer to my actual question here.
My question should have been, "When multiple inputs exist in a single form, how does the browser determine which one is chosen when hitting the enter key?"
The answer is, when the enter key is hit, the first input of type="submit" is chosen. However, IE will skip any submit buttons that are hidden with display:none.
I found the answer here:
Multiple submit buttons on HTML form – designate one button as default
My fix was to set the submit button to position: absolute; left: -1000% rather than display:none. I got that solution from #bobince on the linked answer, however, left:-100% did not push it completely off the page for me so I changed it to left:-1000%.
IMHO it seems wrong to be using a submit button do convey some information other than "hey, I've submitted some data". If the user hits enter to submit the form it is reasonable that some browsers would send all the data associated with all the submit buttons.
If you are just resetting the inputs from previous parts of the form you could use:
<button type="reset">
If you do need other input data maybe a checkbox would be more appropriate:
<form>
...
<input type="checkbox" id="edit-reset" name="op" value="Reset Filters">
<label for="edit-reset">Reset Filters</label>
<div class="submit-button">
<input class="form-submit" type="submit" id="edit-submit-fda-views" name="" value="Submit">
</div>
...
</form>
If you do not need other input data you could use two forms:
<form>
...
<div class="submit-button">
<input class="form-submit" type="submit" id="edit-submit-fda-views" name="" value="Submit">
</div>
</form>
<form>
<div class="reset-button">
<input type="submit" id="edit-reset" name="op" value="Reset Filters" class="form-submit">
</div>
</form>
A submit button is an input. It has a name and a value. When you click on one of the submit buttons, it's value gets added to the the submission with it's name. When you hit the enter key, the form is automatically submitted, but since you are using two submit buttons, they are both contributing a parameter. You have a lot of options that others have already suggested. You could change the type to "reset" or "button", but if you need to post to the server for both actions, then you could intercept the keystroke with javascript and click the button in code. I would probably go with a button type that would submit the form like this.
<input type="button" id="edit-reset" name="op" value="Reset Filters"
class="form-submit" onclick="submitform()">
<Script>
function submitform()
{
document.getElementById("your-form-name-here").submit();
}
</script>
The page I'm developing is this: http://www.thespeaker.ca/
I have a search box in the drop-down menu link (I'm using a drop down after using a lot of other menu styles -- I know the drop down is kind of ugly and quite "beta" but it is light code-wise).
I've tried doing what I use for the clickable "S" button at the left, which is wrapping the image in this span:
<span data-href="http://www.thespeaker.ca/" onclick="document.location.href = this.getAttribute('data-href'); return false;">
But it doesn't seem to work for the search box.
Any other ideas I could try?
If you want to stop the top accordion menu from opening when clicked on the search box, add a onclick="return false" to the input element.
<input value="" name="s" id="s" placeholder="Search" type="text" onclick="return false">
It blocks the onclick event to be sent to parent accordion within the search box.
It seems that my CancellableFormController picks the first of the 2 submit buttons' action if Enter is pressed on the form.
i.e. if my successView is success.jsp and cancelView is cancel.jsp and on my form Cancel button appears before Register, cancel.jsp is called when I hit enter.
Why is this happening. Code for my buttons is:
<input type="submit" name="cancel" value="<spring:message code="submit.cancel"/>"/>
<input type="submit" value="<spring:message code="submit.register"/>"/>
Can I change this?
If you have two separate submit buttons in your jsp does not mean that your controller will perform differently based on that.
The thing here is whenever you submit any of the button it will perform the same action defined in the form tag.
You need to have two separate forms for each submit button and they must have the separate actions defined in it.