2012-08-06 64 views
0

更新: 看着人們的建議,我意識到我應該去沒有AJAX。我決定改變加載函數,現在把jQuery代碼放在裏面。但是,它仍然不起作用,因爲沒有元素顯示爲可見。這是新代碼的樣子。HTML選擇標籤和jQuery顯示和隱藏

<style> 

.input_1 {display: none;} 

.input_2 {display: none;} 

.input_3 {display: none;} 

</style> 

<script type="text/javascript"> 

var numbers = $('#select_number').val(); 

function load(numbers) {  
if (numbers == 1) 
{$("input").hide();$(".input_1").show();} else 
if (numbers == 2) 
{$("input").hide();$(".input_1").show();$(".input_2").show();} else 
if (numbers == 3) 
{$("input").hide();$(".input_1").show();$(".input_2").show();$(".input_3").show();}} 

</script> 

這裏是選擇標籤貌似現在什麼

<form id="form" action="" method="post"> 

<select id="select_number" onclick="load(numbers);" > 

<option>1</option> 
<option>2</option> 
<option>3</option> 

</select> 

<input type="text" class="input_1"/> 

<input type="text" class="input_2"/> 

<input type="text" class="input_3"/> 

</form> 

回答

0

找到了解決辦法。在onclick屬性的加載函數中添加$('#select_number')。val()並且它可以工作。謝謝大家的幫助。

0
<select id="select_number" onclick="load('ajax_file.php', number_of_places);"> 

什麼值是「小數位數」當您單擊的元素?根據你的代碼,它設置一次,永不改變。

一個解決方法就是在發送之前得到的值:

function load(thefile) {  
    var number = $('#select_number').val(); 
    $.get(thefile, {number : number}); 
} 

或者得到函數調用

<select id="select_number" onclick="load('ajax_file.php', $('#select_number').val());"> 

此外,你不執行的任何操作價值結果。也許你想這樣嗎?但如果沒有,你需要添加這樣的東西:

$.get(thefile, {number : number}, function (data){ 
     alert('The results are: ' + data); 
     // do something with the results 
    } 
); 

我希望有幫助!

+0

好吧。當我在我想要結果的ajax文件中寫入show()時。我想要的是用戶點擊select標籤中的數字,然後顯示一些元素。所以我應該把我的ajax文件中的show()jquery函數放入函數(data){... – jason328 2012-08-06 02:11:39

0

我會用jQuery來完成這樣的任務。 jQuery使得使用AJAX調用非常容易。

var myVar = $.get("ajax_file.php", 
        "/*data to be sent to php file (optional)*/", 
        function(data) { /*whatever you want you do (or simply leave it empty if your php does what you need)*/ }, 
        "text" /*data Type*/ 
       ); 
+0

我沒有使用jquery。 – jason328 2012-08-06 02:06:20

0

Jquery無法在服務器上遠程執行,因此jQuery在您的Ajax調用中將不會執行任何操作。這對jquery的本質至關重要,它不是服務器端語言。

function load(thefile) {     
    var number_of_places = $('#select_number').val(); 
    $.get(thefile, {number : number_of_places}, function(numbers){ 
     if (numbers = 1) 
      {$("#input_1").show();} else 
     if (numbers = 2) 
      {$("#input_1").show();$("#input_2").show();} else 
     if (numbers = 3) 
      {$("#input_1").show();$("#input_2").show();$("#input_3").show();} 
    }); 
)}; 

php文件:

<?php 

    if (isset($_GET['number']) === true && empty($_GET['number']) === false) { 
     print $_GET['number']; 
    } 

    ?>