2012-07-20 56 views
4

我构建的JS/Ajax函数没有按钮单击或页面刷新。该函数获取输入字段的值,并用php回显结果。但每次变量都被回显时,下一个变量会擦除前一个变量的值。如何避免这种情况? EXAMPLEPHP/JS:在不丢失值的情况下响应多个变量

JS

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

    $('#contact_name').on('keyup', function() { 
    clearTimeout(timer); 
    timer = setTimeout(submitForm, 050); 
      var name = $("#contact_name").val(); 
    dataString = 'name='+ name; 
    }); 

    $('#email').on('keyup', function() { 
    clearTimeout(timer); 
    timer = setTimeout(submitForm, 050); 
    var name = $("#email").val(); 
    dataString = 'name='+ name; 
    }); 

    $('#phone').on('keyup', function() { 
    clearTimeout(timer); 
    timer = setTimeout(submitForm, 050); 
    var name = $("#phone").val(); 
    dataString = 'name='+ name; 
    }); 

    $('#address').on('keyup', function() { 
    clearTimeout(timer); 
    timer = setTimeout(submitForm, 050); 
    var name = $("#address").val(); 
    dataString = 'name='+ name; 
    }); 

    $('#website').on('keyup', function() { 
    clearTimeout(timer); 
    timer = setTimeout(submitForm, 050); 
    var name = $("#website").val(); 
    dataString = 'name='+ name; 
    }); 


}); 
</script> 

HTML/PHP

<form action="" method="post" enctype="multipart/form-data" id="contact_form" name="form4"> 
    <div class="row"> 
     <div class="label">Contact Name *</div> <!-- end .label --> 
     <div class="input"> 
      <input type="text" id="contact_name" class="detail" name="contact_name" value="<?php $contact_name ?>" /> 
      <div id="special"><span id="resultval"></span></div> 
     </div><!-- end .input--> 
    </div><!-- end .row --> 
    <div class="row"> 
     <div class="label">Email Address *</div> <!-- end .label --> 
     <div class="input"> 
     <input type="text" id="email" class="detail" name="email" value="<?php $email ?>" /> 
     <div id="special"><span id="resultval"></span></div> 
     </div><!-- end .input--> 
    </div><!-- end .row --> 
    </form> 

回答

4

您可以使用append()方法:

success: function(result){ 
     $('#special').append('<p>' + result + '</p>'); 
} 

为您设置同级ES的输入,你可以运行如下代码:

$('.detail').on('keyup', function() { 
    clearTimeout(timer); 
    var name = $(this).val(); 
    dataString = 'name='+ name; 
    timer = setTimeout(submitForm, 050); 
}); 

注意标识必须是唯一的,反复向服务器请求数据的效率不高。

+0

谢谢!关于ID的一个问题:我如何使ID唯一? – CodingWonders90 2012-07-20 05:47:05

+0

@cholomanCoding欢迎你,他们不应该是类似的,例如你有两个id为'#special'的元素,你可以把它们改成'#special1'和'#special2',或者简单地使用一个类如'.special '。 – undefined 2012-07-20 05:49:38

+0

噢,好的。因此,如果我将div更改为'

'和'
',我还需要添加:$('#special1')。append('

'+ result +'

');' '$(' (''#special2')。append('

'+ result +'

');' – CodingWonders90 2012-07-20 06:05:09

1

您所遇到的问题是由于对多个元素使用相同的ID造成的。

ID应该是唯一的,但如果有相同的ID,只有第一个具有这种ID的元素被选中。

+0

+1我是新来的学习者。我怎样才能让这些ID独一无二? – CodingWonders90 2012-07-20 05:48:30

+0

通过为每个元素使用不同的id,可以让id唯一。在你的表单示例中,你可以使用special_email,special_contact_name等... – Leon 2012-07-20 06:51:45

相关问题