Php 使用Ajax和单选按钮切换可见性

Php 使用Ajax和单选按钮切换可见性,php,ajax,Php,Ajax,在我的page1.php上,我想切换page2.php的可见性 我正在使用以下代码: <input type="radio" name="city" value="Barcelona" onclick="getPage2(this.value);"> Barcelona <input type="radio" name="city" value="Manchester"> Manchester 我只想在选中单选按钮时显示page2.php,如果没有单选按钮或“Mache

在我的page1.php上,我想切换page2.php的可见性

我正在使用以下代码:

<input type="radio" name="city" value="Barcelona" onclick="getPage2(this.value);"> Barcelona
<input type="radio" name="city" value="Manchester"> Manchester
我只想在选中单选按钮时显示page2.php,如果没有单选按钮或“Machester”单选按钮被选中,则不显示(隐藏它)

$("input[type='radio']").click(function()
{
    if($(this).val() == "Barcelona") $("#content").show();
    else $("#content").hide();

});

更新

我解决了。其实很简单

<input type="radio" name="city" value="Barcelona" onclick="getPage2(this.value);"> Barcelona
<input type="radio" name="city" value="Manchester" onclick="getPage2(this.value);"> Manchester

无论如何还是要感谢大家。

您可以使用JQuery选择器:

$('input').click(function(){
    $('input').show(); // Show all inputs
    $(this).hide(); // Hide the clicked input

    $.ajax({
        type: "GET",
        url: "page2.php",
        success: function(result){
            $("#show_results").html(result);
        }
    });
})
当然,这只适用于只包含这些输入的页面

您应该为您的收音机设置类或ID,以便使用选择器直接将它们作为目标

您将有如下内容:


$('.radio page')。单击(function(){//…

您可以使用JQuery选择器执行以下操作:

$('input').click(function(){
    $('input').show(); // Show all inputs
    $(this).hide(); // Hide the clicked input

    $.ajax({
        type: "GET",
        url: "page2.php",
        success: function(result){
            $("#show_results").html(result);
        }
    });
})
当然,这只适用于只包含这些输入的页面

您应该为您的收音机设置类或ID,以便使用选择器直接将它们作为目标

您将有如下内容:


$('.radio page')。单击(function(){//…

您可以将可隐藏的内容与原始页面一起加载。这样可以避免ajax调用

<input class="radios" type="radio" name="city" value="Barcelona"> Barcelona
<input class="radios" type="radio" name="city" value="Manchester"> Manchester
<div id="content" style="display:none">Hide-able content</div>

您可以在原始页面中加载可隐藏的内容,这样可以避免ajax调用

<input class="radios" type="radio" name="city" value="Barcelona"> Barcelona
<input class="radios" type="radio" name="city" value="Manchester"> Manchester
<div id="content" style="display:none">Hide-able content</div>

嗨,Max,我不想隐藏单击的输入。我想隐藏我在单击单选按钮BarcelonaHi Max时调用的page3.php。我不想隐藏单击的输入。我想隐藏我在单击单选按钮BarcelonaHi Sanckke时调用的page3.php,我想隐藏page2.php而不是div。@SunZ你能不能把cont将page2.php的内容隐藏在div中?是的,我可以。但是我想学习如何隐藏另一个页面的内容。这对我的项目会有很大帮助。我测试了你的示例,但不起作用。你能检查一下吗?@SunZ我很抱歉我有一个错误,我更新了代码。嗨,桑克,我想隐藏page2.php而不是div。@SunZ你不能把p的内容放进去吗是的,我可以。但我想学习如何隐藏另一个页面的内容。这将对我的项目有很大帮助。我测试了你的示例,但不起作用。你能检查一下吗?@SunZ我很抱歉我有一个错误,我更新了代码。