开发者

form button, ajax, enter key, form submit

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"

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?

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;
    v开发者_开发技巧ar carat = document.getElementById('carat').value;

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

}

//-->


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

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


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

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜