2012-07-20 57 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

相關問題