简体   繁体   English

JS / AJAX:提交时没有刷新或按钮点击没有触发

[英]JS/AJAX: submit without refresh or button clicking not firing after input

The js function will not work in my SITE but when I try it in JSFIDDLE it shows to work fine. js函数在我的网站上不起作用,但是当我在JSFIDDLE中尝试它时,它显示工作正常。 I have included DOM ready but it doesn't trigger anything. 我已经准备好了DOM但它没有触发任何东西。 I checked with firefox and input value is not posted at all. 我用firefox检查过,根本没有发布输入值。 How can I get the JS function to fire up properly after user stops typing? 如何在用户停止输入后正确启动JS功能?

JS JS

<script>
$(document).ready(function() {
 var timer = null; 
 var dataString;   
 function submitForm(){
   alert("success");
   $.ajax({ type: "POST",
            url: "index.php",
            data: dataString,
            success: function(result){
              alert("success");
               /*$('#special').append('<p>' +  $('#resultval', result).html() + '</p>');*/

            }

   });
   return false;
 }
 $('#submit').on('keyup', function() {
    clearTimeout(timer);
    timer = setTimeout(submitForm, 2000);
    var name = $("#name").val();
    dataString = 'name='+ name;
 });
 }); 
</script>

HTML HTML

 <input id="name" name="name" type="text" class="field text medium" value="<? $url ?>" maxlength="255" tabindex="1"     />

The problem is that in the HTML above you have id="name" but are binding to "#submit" . 问题是在上面的HTML中你有id="name"但是绑定到"#submit"

You have id="submit" in your jsfiddle example, which is why it works. 你的jsfiddle例子中有id="submit" ,这就是它工作的原因。

在您的网站上,我找不到ID为“submit”的元素,因此$('#submit')返回空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM