使用onclick选项时,Ajax无法使用HTML表单中的“提交"按钮 [英] Ajax is not working with submit button in HTML form while using the onclick option

查看:47
本文介绍了使用onclick选项时,Ajax无法使用HTML表单中的“提交"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我仅使用输入文本字段时,以下代码有效,但当我使用提交按钮时,则无效.我不知道jquery,所以请解释

The code below works when i use just the input text field but it doesn't works when i use a submit button. I don't know jquery so please explain

<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str) {
 if (str.length == 0) { 
    document.getElementById("txtHint").innerHTML = "";
    return;
} else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", "download.php?q="+str, true);
    xmlhttp.send();
    }
    }
  </script>
 </head>
 <body>

 <p><b>Start typing a name in the input field below:</b></p>
 <form method="POST" action="showHint(str)"> 
 First name: <input type="text" placeholder="Copy Your Facebook Video URL Over Here" name = "URL" onclick="showHint(URL.value)" >
 <button type="submit" onclick="showHint(URL.value)">Download</button>
 </form>
 <p>Suggestions: <span id="txtHint"></span></p>
 </body>
 </html>

但是,当我使用提交"按钮时,它不起作用,请帮助

However when i use the submit button instead it doesn't works Please help

推荐答案

发生了同样的事情,而不是使用按钮,而是使用输入类型=按钮,如下所述-

Same thing happened with me instead of using button use input type = button like described below -

<input type="button" id="submit" value="submit" name="submit" onclick="showHint(URL.value)"></input>

它肯定会工作

这篇关于使用onclick选项时,Ajax无法使用HTML表单中的“提交"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆