表单按钮,ajax,输入键,表单提交

时间:2022-04-19 09:08:07

i have the above on a page, if i click the button, my ajax function works fine. however, if i press "enter", the form submits but nothing happens - no doubt as the function is called "onclick"

我在页面上有上面的内容,如果我点击按钮,我的ajax功能就会运行得很好。然而,如果我按下“enter”,表单就会提交,但什么也没有发生——毫无疑问,这个函数被称为“onclick”

is there any way to change this so the user may click the button, or press enter on the keyboard, and have the form submit to ajax correctly?

有什么办法可以改变它,让用户点击按钮,或者按键盘上的enter键,让表单正确提交给ajax吗?

thanks

谢谢


updated:

更新:

    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }
    var gold = document.getElementById('gold_amount').value;
    var carat = document.getElementById('carat').value;

    var queryString = "?gold=" + gold + "&carat=" + carat;
    ajaxRequest.open("GET", "script.php" + queryString, true);
    ajaxRequest.send(null); 

}

//-->

2 个解决方案

#1


3  

you have the onsubmit event on your form tag. it's raised when the form si submited (so also when you press enter).

在表单标记上有onsubmit事件。当表单si submited(也就是当您按enter时)时,它就会被激活。

http://www.w3schools.com/jsref/event_form_onsubmit.asp

http://www.w3schools.com/jsref/event_form_onsubmit.asp

#2


0  

Javascript has a method onKeydown and with that you can capture a Enter key click.

Javascript有一个onKeydown方法,您可以用它捕获一个Enter键单击。

<body>
<form action="" method="POST" id="myForm">
<input type="text" name="myText" onKeyDown="changeVal()">

<script type="text/javascript" language="JavaScript">
s1 = new String(myForm.myText.value)

function changeVal() {
     s1 = "You pressed a key"
    myForm.myText.value = s1.toUpperCase() 
} 

</script>
</form>
</body>

#1


3  

you have the onsubmit event on your form tag. it's raised when the form si submited (so also when you press enter).

在表单标记上有onsubmit事件。当表单si submited(也就是当您按enter时)时,它就会被激活。

http://www.w3schools.com/jsref/event_form_onsubmit.asp

http://www.w3schools.com/jsref/event_form_onsubmit.asp

#2


0  

Javascript has a method onKeydown and with that you can capture a Enter key click.

Javascript有一个onKeydown方法,您可以用它捕获一个Enter键单击。

<body>
<form action="" method="POST" id="myForm">
<input type="text" name="myText" onKeyDown="changeVal()">

<script type="text/javascript" language="JavaScript">
s1 = new String(myForm.myText.value)

function changeVal() {
     s1 = "You pressed a key"
    myForm.myText.value = s1.toUpperCase() 
} 

</script>
</form>
</body>