2011-11-18 121 views
2

我使用jQuery Mobile,我有一個選擇框,選擇是和否作爲選項。當他們選擇「是」時,我想要顯示文本輸入字段,並在選擇「否」時消失。選擇顯示與jQuery文本輸入

jsFiddle link

<fieldset data-role="controlgroup"> 
<label for="joint">Is this a joint gift?</label> 
<select name="slider" id="joint" data-role="slider"> 
    <option value="no">No</option> 
    <option value="yes">Yes</option> 
</select> 
<div id="enter_joint"> 
    <label for="other_joint">Spouse Name:</label> 
    <input name="other_joint" id="other_joint" value="" data-theme="d" /> 
</div> 
</fieldset> 

我希望在選擇設置爲是有使用jQuery只顯示#enter_joint。

回答

1

AlienWebguy的回答會的工作,但我傾向於犯錯的「檢查值」的一面。因此,這裏再檢查一下值,然後確定是否隱藏文本字段。

$(document).ready(function() { 
    $("#enter_joint").hide(); 
    $("#joint").val('no'); 
    $("#joint").change(function() { 
     if ($(this).val() == 'yes') { 
      $("#enter_joint").show(); 
     } else { 
      $("#enter_joint").hide(); 
     } 
    }); 
}); 

jsfiddle

+0

使用切換功能,更有效,其中一個較少的代碼給你相同的功能。 $( '#enter_joint')切換( '秀'); –

+0

@Saifuddin,在這種情況下,切換可能是最好的路線,因爲只有2個答案(這就是爲什麼我+1了AlienWebguy的答案)。儘管有這種情況,但有些時候你想明確表達。 :) – villecoder

1
$(document).ready(function(){ 
    $('#enter_joint').hide(); 
    $('#joint').change(function(){ 
     val = $(this).val(); 

     if(val == 'yes') 
     { 
      $('#enter_joint').show(); 
     } 

     if(val == 'no') 
     { 
      $('#enter_joint').hide(); 
     } 
    }); 
}); 
0

試試這個

$('#joint').change(function(event) { 
    if($('#joint').val() === 'yes') { 
     $('#enter_joint').hide(); } 
    else { $('#enter_joint').show(); 
      } 
}); 
0

替代語法:

$('#enter_joint').hide(); 
$('#joint').change(function(){ 
    show = $(this).val() == 'yes'; 
    $('#enter_joint').toggle(show); 
}); 
相關問題