jquery - enter key do something javascript -
i using bootstrap , jquery. when enter tracking field hit enter key nothing happens. because not using attribute. if write press check button loads modal , executes checkform() function. question: want same thing when press button. how can this?
html:
<input type="text" placeholder="tracking kod" name="tracking" id="tracking" class="form-control" autocomplete="off" required> <button type="submit" class="btn btn-success" id="checkbutton" onclick="checkform();" data-toggle="modal" data-target="#mymodal"> <span class="glyphicon glyphicon-check"></span> check </button>
javascript:
function checkform() { var tracking = document.getelementbyid("tracking"); var modal = document.getelementbyid("modal"); if(tracking.value == '') { modal.innerhtml = '<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove"></span> xahiş edirik tracking daxil edin!</div>'; return false; } else { modal.innerhtml = '<div class="alert alert-info" role="alert"><span class="fa fa-spinner fa-spin"></span> gözləyin</div>'; } var url = 'track.php?tracking=' + tracking.value + ''; $.get(url, function(response) { $( "#modal" ).hide(0.1); $( "#modal" ).show( "slow" ); modal.innerhtml = response; }); }
i tried code. added end of body tag:
<script> $.fn.enterkey = function (fnc) { return this.each(function () { $(this).keypress(function (ev) { var keycode = (ev.keycode ? ev.keycode : ev.which); if (keycode == '13') {fnc.call(this, ev);} }) }) } $("#tracking").enterkey(function () {checkform();}); </script>
<script type="text/javascript"> $.fn.enterkey = function (fnc) {return this.each(function () {$(this).keypress(function (ev) { var keycode = (ev.keycode ? ev.keycode : ev.which); if (keycode == '13') {document.getelementbyid("checkbutton").click();}})}) } $("#tracking").enterkey(function () {checkform();}); </script>
Comments
Post a Comment