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

时间:2022-11-19 21:33:31

The js function will not work in my SITE but when I try it in JSFIDDLE it shows to work fine. I have included DOM ready but it doesn't trigger anything. I checked with firefox and input value is not posted at all. How can I get the JS function to fire up properly after user stops typing?

js函数在我的网站中不起作用,但是当我在JSFIDDLE中尝试它时,它显示工作正常。我已经准备好了DOM,但它没有触发任何东西。我用firefox检查过,输入值根本没有发布。如何在用户停止输入后正确启动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

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

2 个解决方案

#1


3  

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”,这就是它的工作原理。

#2


1  

On your site, I can't find an element with id "submit" so $('#submit') returns empty.

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

#1


3  

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”,这就是它的工作原理。

#2


1  

On your site, I can't find an element with id "submit" so $('#submit') returns empty.

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