我有簡單的 ajax 腳本,它從表單發送資料,它可以使用這個按鈕,我想在按 Enter 時發送日期,我該怎么做?很感謝!
ajax代碼
jQuery(document).ready(function(){
jQuery('.ajaxSubmit').click(function(e) {
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{ url('/names') }}",
method: 'get',
data: {
runBy: $(this).closest("form").find(".name").val(),
command: $(this).closest("form").find(".surname").val(),
},
success: function() {
location.reload();
}
});
});
});
php代碼
<form>
<input type="hidden" value="{{Auth::user()?->name}}" class="name">
<textarea class="surname" name="input" cols="30" rows="1"> </textarea>
<button class="ajaxSubmit" onclick="event.preventDefault()">send</button>
</form>
uj5u.com熱心網友回復:
將您的 Ajax 提交代碼移動到單獨的函式中。將 Keypress 事件添加到輸入元素。當 keypress 值為 13 時,從 keypress 事件中呼叫該函式。下面是供您參考的代碼。
$('.input').keypress(function (e) {
if (e.which == 13) {
// Call the callback function
return false;
}
});
通過單擊按鈕呼叫相同的 Ajax 函式。
問候,親
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/498250.html
標籤:javascript 阿贾克斯
