2012-01-16 76 views
0

這顯示選定單選按鈕上的結果。我有一個問題,當頁面刷新沒有div顯示和結果顯示。我想在頁面刷新時顯示Carsbuy div。顯示/隱藏div上的單選按鈕結果顯示在身上onload

$(document).ready(function() { 
     $("input[name$='mode']").click(function() { 
      var test = $(this).val(); 

      $("div.desc").hide(); 
      $("#Cars" + test).show(); 
     }); 
    }); 

<input type="radio" name="mode" checked="checked" value="buy" /> 

<label>Buy</label> 

<input type="radio" name="mode" value="rent" /> 

<Label>Rent</label> 

<div id="Carsbuy" class="desc" style="display: none;"> 

    Buy Cars Result Display Here On Select Buy 

</div> 

<div id="Carsrent" class="desc" style="display: none;"> 

    Rent Cars Result Display Here On Select Rent 
</div> 
+0

它不顯示結果,對身體的負荷PLZ檢查烏爾編輯的代碼@NAVEED – user1138383 2012-01-16 07:00:17

+0

能否請您解釋一下什麼是你要求的......我不明白你的正確的語言,你想在負載或不負載時顯示'div'。 – Murtaza 2012-01-16 07:04:24

回答

0

儘管我明白你的要求,你可以這樣做,如下所示。

HTML:

<input type="radio" name="mode" checked="checked" value="buy" /> 
<label>Buy</label> 
<input type="radio" name="mode" value="rent" /> 
<Label>Rent</label> 
<div id="CarsDetails" class="desc"> 
    // include buy type cars initialy while page load. 
</div> 

JQuery的:

$(document).ready(function() { 
    $('input[type="radio"]').click(function() { // detect when radio button is clicked 
    var carType = $(this).val(); // get the value of selected radio type 
    $.ajax({ 
     url : "cartype.php", // link to file containing data of cars 
     type : "GET", // request type 
     data : "carType="+carType, // send the selected car type (buy or rent) 
     datatype : "html", 
     success : function(response){ // get the response 
      $('#CarsDetails').html(response); // put the response in div. 
     } 
    }); 
    }); 
}); 
+0

好吧,當我submiteed形式我想選擇那個按鈕 – user1138383 2012-01-16 07:01:34

+0

沒有讓你..?你在說什麼形式和按鈕?請更具描述性。還請檢查我在回答中提供的小提琴。 – 2012-01-16 07:05:03

+0

我有一個搜索表單,其中搜索租金和購買汽車。現在我想要如果我搜索租車租金將檢查subitted搜索表單..你明白嗎? – user1138383 2012-01-16 07:07:57

1

你可以在你的js代碼開頭使用下列鞋企具體的DIV在頁面加載:

$("#Carsbuy").show(); 

編輯:

特定DIV和單選按鈕,使用下列內容:

var selected = 'rent'; 
$("div.desc").hide(); 
$("#Cars" + selected).show(); 
$('input[value="' + selected + '"]').attr('checked', true); 

可以更改var selected值來選擇特定的單選按鈕,DIV

Demo

+0

好吧,現在我想在頁面刷新後顯示選定的按鈕。我的意思是如果一個選擇的租金和刷新頁面顯示頁面刷新租? – user1138383 2012-01-16 07:11:04

+0

看看編輯答案 – NAVEED 2012-01-16 07:14:46

+0

thanx這個答案非常有幫助 – user1138383 2012-01-16 07:21:02

0

在下面的代碼中,在頁面加載時檢查是否選擇了單選按鈕,然後顯示相應的div。如果使用條件,以防止如果沒有單選按鈕被選中

$(document).ready(function() { 
var selected = $("input[type='radio']:checked").val(); 

if(selected !== 'undefined') 
{ 
    $("#Cars" + selected).show(); 
} 

$("input[type='checkbox']:checked").attr('id') 
    $("input[name$='mode']").click(function() { 
     var test = $(this).val(); 

     $("div.desc").hide(); 
     $("#Cars" + test).show(); 
    }); 
}); 
相關問題