2012-01-16 46 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(); 
    }); 
});