I have a form that uses a calendar with an input field. For some reason when I press the Enter key while on the input field it doesn't submit the form. Instead the focus is moved onto the calendar icon in the form. I tried changing the type to = submit and to button, but the behavior doesn't change. What can I do to make sure that the form gets submitted while the user is on the input field?
<form id="form" name="form" action="process">
<div class='row' tal:condition="python:op in ['search', 'add', 'view']">
<div style="width: 5em;"><label for="date">Date</label></div>
<div tal:content="structure python:container.util.getDateTimeInputsA11y(form_name='form', my_date=DateTime().strftime('%m-%d-%Y'), date_field='date',type_date=0, date_arrows=1, use_clear='yes')"></div>
</div>
<div class='row first_child' tal:condition="python:op in ['add']">
<div><label for="time">Time</label></div>
<div><input type="text" id="time" name="time" tal:attributes="value python:DateTime(fix_date).strftime('%H%M')" maxlength="4" nofocus="1" /></div>
</div>
<div class='row first_child' tal:condition="python:op in ['search', 'add', 'unit']">
<div><label for="unit">Unit</label></div>
<div><a tal:define="opt_select_cond python:'unit.patients=\'Y\'';"
tal:replace="structure python:container.util.getUnitSelects(my_form='form', my_var='unit', my_var_value=unit, unit_type='sch_unit', text_input=1, multiunit=0, opt_select_cond=opt_select_cond)" /></div>
</div>
<div class='row first_child' tal:condition="python:op in ['add', 'edit']">
<div><label for="room">Room</label></div>
<div><input type="text" id="room" name="room" tal:attributes="value python:str(bed.get('room') or '').strip();" /></div>
</div>
<div class='row first_child' tal:condition="python:op in ['add', 'edit']">
<div><label for="bed">Bed</label></div>
<div><input type="text" id="bed" name="bed" tal:attributes="value python:str(bed.get('bed') or '').strip();" /></div>
</div>
<div>
<div>
<input type="hidden" id="op" name="op" tal:attributes="value python:op;" />
<input type="hidden" id="bed_id" name="bed_id" tal:attributes="value python:int(request.get('bed_id') or 0);" />
<input type="button" class="btn btn-light" id="submit_btn" tal:attributes="value python:op.title();" />
</div>
</div>
</form>
I also noticed that the tal:replace structure calls this print line in a python file.
print """
<input type="%s" name="%s" id="%s" aria-label="%s" class="uppercase" value="%s"
onfocus="this.select();"%s size="10" maxlength="10">
""" % (test(text_input,'text','hidden'), my_var, my_var, my_var_title, my_var_value, test(onchange, ' onchange="%s"'%(onchange), ''))
I removed the onfocus on that print line but same issue still occurs.
For any form to submit after enter, you need to have either:
<button type="submit"></button>
or:
<input type="submit" />
in it which is missing in your code.
It's working now after I added a script tag for event listener. Still unclear why pressing Enter key changes the focus to calendar.
let unit_input = document.querySelector('#unit');
unit_input.addEventListener('keydown', (e) => {
switch (e.key)
{
case 'Enter':
document.getElementById('submit_btn').click();
}
Related
I try to auto calculate html input values to show total values in input .
here is my input form
<div class="col-sm-4">
<form>
<div class="mb-3">
<label for="">Price</label>
<input type="text" id="price" required class="price form-control">
</div>
<div class="mb-3">
<label for="">Amount</label>
<input type="text" id="amount" required class="amount form-control">
</div>
<div class="mb-3">
<label for="">Total</label>
<input type="text" id="total" required class="total form-control">
</div>
<button type="submit" class="btn btn-primary marketbuy">Buy </button>
</form>
</div>
I want total value automatically when enter amount value and price value into input form.
example
price is 3
amount is 5
total is 15
when I enter 3 and 5
total input show automatically 15
var p1 = $('#price').val();
var p2 = $('#amount').val();
$('#total').val(p1*p2);
console.log()
this code not show directly in total input but show when reload.
How can get by jquery.
The issue is because your code is only running when the page loads. You need to hook event handlers to the input fields so that the total also gets calculated as the user enters values.
Below is a working example of how to do this. Note that the common logic which updates the total field is extracted to its own function which is called when the page loads and also when a field is updated. Also note that I added the required attribute to the total field so that the user cannot update it - it can only be set programmatically.
let $price = $('#price');
let $amount = $('#amount');
let updateTotal = () => $('#total').val($price.val() * $amount.val());
updateTotal(); // on page load
$('form input').on('input', updateTotal); // on value change
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="col-sm-4">
<form>
<div class="mb-3">
<label for="">Price</label>
<input type="text" id="price" required class="price form-control" />
</div>
<div class="mb-3">
<label for="">Amount</label>
<input type="text" id="amount" required class="amount form-control" />
</div>
<div class="mb-3">
<label for="">Total</label>
<input type="text" id="total" required class="total form-control" readonly />
</div>
<button type="submit" class="btn btn-primary marketbuy">Buy</button>
</form>
</div>
$('form input').on('keyup',function(){
$('#total').val(parseInt($('#price').val()*$('#amount').val()));
});
<!-- The issue is because your code is only running when the page loads. You need to hook keyup event handlers to the input fields so that the total also gets calculated as the user enters values.-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="col-sm-4">
<form>
<div class="mb-3">
<label for="">Price</label>
<input type="text" id="price" required class="price form-control" />
</div>
<div class="mb-3">
<label for="">Amount</label>
<input type="text" id="amount" required class="amount form-control" />
</div>
<div class="mb-3">
<label for="">Total</label>
<input type="text" id="total" required class="total form-control" readonly />
</div>
<button type="submit" class="btn btn-primary marketbuy">Buy</button>
</form>
</div>
I have a small HTML form created that has a required input field; when I hover the mouse over the field, it shows the message "Please fill in this field" but when I click the button to submit, the error message does not display.
Here is the section of code containing the input field and button:
Web Word Count App
</div>
<div>
<textarea id="paragraph" rows="5" cols="35" placeholder="Enter the paragraph here..." value="" required="true" >Enter the paragraph here...
</textarea>
</div>
<div>
<input type="text" id="word" placeholder="Enter the keyword here..." value="" required="true" >
</div>
<div>
<input type="text" class="display" id="display-1" readonly=1 placeholder="Total word count = 0 " value=""><br>
<input type="text" class="display" id="display-2" readonly=1 placeholder="Keyword does not exist!" value=""><br>
<input type="text" class="display" id="display-3" readonly=1 placeholder="Total keyword appearances = 0" value="">
</div>
<div>
<button class="wwcbutton" onclick="Word_count();">Total words?</button>
</div>
<div>
<button class="wwcbutton" onclick="Check();">Check keyword appearance</button>
</div>
<div>
<button class="wwcbutton" onclick="keyword_count();">Total keyword appearances?</button>
</div>
<div>
<button class="wwcbutton-clear" onclick="Clear();">Clear</button>
</div>
Could someone please advise me as to what might be wrong here. Thanks
I have a code that submits form by clicking <input type="submit">, and I want to change my form to submit when label, which is outside of the form, is clicked.
What should I do?
Here's my code:
<div class="join">
<p>Join</p>
<span>Join to access all features of the site!</span>
</div>
<form action="join/joinf" method="POST" class="joinform">
<div class="left">
<p>ID<span class="required"> *required</span></p>
<p>PW<span class="required"> *required</span></p>
</div>
<div class="right">
<p><input type="text" name="id" id="id" required>
<p><input type="password" name="pw" required></p>
</div>
<input type="submit" id="formsub" value="join">
</form>
<label for="formsub"><button>join-label</button></label>
Try using Jquery click element, set id for label .
$('#LabelId').click(function(e) {
e.preventDefault();
$("#formsub").submit();
});
I'm on Home/Index. I have the following HTML:
<form id="frmCode" style="display: inline-block">
<input type="text" name="ConfirmationCode"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
For some reason, if I have the cursor in the ConfirmationCode input and I press the Enter key, the form submits, and redirects to http://localhost:62500/?ConfirmationCode= . The thing is, I've read about this behaviour and I understood it might be somewhat intended behaviour depending on browser and whatnot. But I have this other form,
<form id="frmLogin" class="form-horizontal" role="form">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-sm-8">
<input type="text" name="MailOrUsername" title="Te poți loga introducând mail-ul sau numele de utilizator" data-val="true" data-val-required="De ce apeși aiurea? Bagă ID." class="form-control" placeholder="Mail sau ID" />
</div>
<div class="col-sm-8">
<input type="password" name="Password" title="Introdu parola asociată contului tău" data-val="true" data-val-required="Bagă parola." class="form-control" placeholder="Parola" />
</div>
<input type="checkbox" name="RememberMe" />
<span title="Bifând căsuța asta rămâi autentificat și după ce închizi browserul">Ține-mă minte</span>
<input type="button" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
<input type="button" onclick="login_hide()" />
<img src="~/Images/loading.gif" id="loginLoading" />
</form>
which doesn't have this behaviour, and nothing happens when I press the Enter key.
The form submits because you hace only 1 input in your form (no additional data needs to be entered).
Change you first form to :
<form id="frmCode" style="display: inline-block">
<input type="text" name="ConfirmationCode"/>
<input type="text" name="ConfirmationCode2"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
and the form will not submit when you press Enter
If you want to disable the submition functionality of the form you can add the onsubmit event handler like this:
<form id="frmCode" style="display: inline-block" onsubmit="return false;">
<input type="text" name="ConfirmationCode"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
You need to set the type of the button you want to use to submit your form to submit.
<input type="submit" ...>
<form id="frmLogin" class="form-horizontal" role="form">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-sm-8">
<input type="text" name="MailOrUsername" title="Te poți loga introducând mail-ul sau numele de utilizator" data-val="true" data-val-required="De ce apeși aiurea? Bagă ID." class="form-control" placeholder="Mail sau ID" />
</div>
<div class="col-sm-8">
<input type="password" name="Password" title="Introdu parola asociată contului tău" data-val="true" data-val-required="Bagă parola." class="form-control" placeholder="Parola" />
</div>
<input type="checkbox" name="RememberMe" />
<span title="Bifând căsuța asta rămâi autentificat și după ce închizi browserul">Ține-mă minte</span>
<input type="submit" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
<input type="button" onclick="login_hide()" />
<img src="~/Images/loading.gif" id="loginLoading" />
</form>
More infos on the input element and on control types.
That is the usual browser behaviour, when a user presses enter when using a form.
The form will submit and as there is no action attribute in your form tag, it will go back to the current URL (with any of the form fields/values attached) ?ConfirmationCode=
As mentioned in 'Ubiquitous Developers' comment; your are using html buttons <input type="button"/> and not the html submit button <input type="submit" />. This will not have the default behaviours of the submit button.
Its probable that the writer of the script you had, didn't want to use the default behaviours of html forms. Instead using javascript to decide how the form will behave.
I think that they use some javascript to prevent the submission of the form like this;
How to prevent ENTER keypress to submit a web form?
The first form is a simple form which follows the default browser rules. The rule is pressing enter means clicking the submit button.
<input type="button"/>
above is not a valid submit button, so the browser submits the form.
In the second case the scenario is diffrerent.
<input type="submit" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
here this is a valid submit button and so in pressing enter key, the browser clicks the button and this button instantly calls the JavaScript function "login()". Now it must be in the code that it does something that prevents the form from submission. may be something like "return false;". So the form doe snot submit.
You can try this. it works
<form method="post" action="yourpage.html">
<input type="text" name="name" class="inputbox" />
<button type="submit" id="btn1">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script>
$(".inputbox").on('keyup', function (e) {
if (e.keyCode === 13) {
$("#btn1").click();
}
});
</script>
I have a new login page which is not submitting. :(
If I click the button and the required fields are not entered, it fires me back the errors, but as soon as they're filled and I try to submit it does nothing.
The page is live at https://www.safewise.co.nz/trainwise/login
The code
<form action="login.php" method="post" name="logForm" id="logForm" class="styled_form">
<?php if(isset($redirectURL) && !empty($redirectURL)) { ?><input type="hidden" id="redirect" name="redirect" value="<?php echo $redirectURL; ?>" /><?php } ?>
<div>
<p id="login-title">Login</p>
<input name="userEmail" type="email" class="validate[required]" id="userEmail" placeholder="Email"<?php if(isset($enteredEmail) && !empty($enteredEmail)){echo ' value="'.$enteredEmail.'"';} ?> required /><span class="form_hint">Email Address</span>
<input name="userPwd" type="password" class="validate[required]" placeholder="Password" id="userPwd" required /><span class="form_hint">Password</span>
<div class="login-checkbox"><input type="checkbox" name="remember" id="remember" style="vertical-align:middle;" /> <label for="remember">Remember Me</label></div>
<p><input name="doLogin" type="submit" id="doLogin" value="Continue" class="form-button" /></p>
<div id="forgot"><p>Register</p><p>Forgotten Password?</p>
</div>
</form>
<div id="forgotPass" class="reveal-modal">
<div class="modal-header"><h3>Forgot Password</h3></div>
<div class="modal-body" style="text-align:center;">
<form action="login.php" method="post" name="forgotForm" id="forgotForm" >
<p style="margin-bottom:4px;"><em style="font-size:small;color:#3A3A3A">Enter your email address below to receive your new password.</em></p>
<p><input name="rstEmail" type="text" class="validate[required]" id="rstEmail" placeholder="john#example.com" size="25" /> <input name="doReset" type="submit" id="doReset" value="Reset" class="form-button" /></p>
</form>
</div>
<a class="close-reveal-modal">×</a>
</div>
The weird thing is as soon as I remove this code it works.
<div id="forgotPass" class="reveal-modal">
<div class="modal-header"><h3>Forgot Password</h3></div>
<div class="modal-body" style="text-align:center;">
<form action="login.php" method="post" name="forgotForm" id="forgotForm" >
<p style="margin-bottom:4px;"><em style="font-size:small;color:#3A3A3A">Enter your email address below to receive your new password.</em></p>
<p><input name="rstEmail" type="text" class="validate[required]" id="rstEmail" placeholder="john#example.com" size="25" /> <input name="doReset" type="submit" id="doReset" value="Reset" class="form-button" /></p>
</form>
</div>
<a class="close-reveal-modal">×</a>
</div>
Any suggestions?
Where is that div located in the page? It is incorrect to have a form inside a form. And so your code may work when you remove the div above because you've taken out the second form.
Consider having them as two separate entities instead of nested.
Are these forms nested? You cannot have nested forms. You can have multiple forms in a page, just as long they're not nested.
Here's some ref on it http://www.w3.org/MarkUp/html3/forms.html
I was missing a closing div
<div id="forgot">
<p>Register</p>
<p>Forgotten Password?</p>
Needs a closing div at end
<div id="forgot">
<p>Register</p>
<p>Forgotten Password?</p>
</div>