Jquery单选按钮总是评估为true

时间:2014-03-26 18:46:35

标签: jquery if-statement radio-button

我有两个单选按钮,一个带值=" true"一个值="假"。当我尝试在if语句中使用该值时,我总是得到" true"无论选择哪一个。

你能指出我的错误吗?

表格摘要:

<p>
            <label class="description" for="wpbpp_settings[slideshow]"><?php _e('Do you want the slider to advance automatically?', 'wpbpp_domain'); ?></label>
            <input id="wpbpp_settings[slideshow]T" name="wpbpp_settings[slideshow]" type="radio" <?php if($wpbpp_options['slideshow'] == 'true') echo 'checked="checked"'; ?> value="true" />Yes
            <input id="wpbpp_settings[slideshow]F" name="wpbpp_settings[slideshow]" type="radio" <?php if($wpbpp_options['slideshow'] == 'false') echo 'checked="checked"'; ?> value="false" />No

         </p>
         <p>   
            <label class="description" for="wpbpp_settings[slideshowSpeed]"><?php _e('How often do you want the slider to advance (number of seconds)?', 'wpbpp_domain'); ?></label>
            <input id="wpbpp_settings[slideshowSpeed]" name="wpbpp_settings[slideshowSpeed]" type="number" min="1" max="8" step="1" value="<?php echo $wpbpp_options['slideshowSpeed']; ?>"/>
        </p>

jquery的:

<script  type="text/javascript">

jQuery(document).ready(function($) {

  togglefields();

  $('input[name="wpbpp_settings[slideshow]"]').change(function(){ alert (jQuery('input[name="wpbpp_settings[slideshow]"]').val()); });

});   

  function togglefields() { 
   if (jQuery('input[name="wpbpp_settings[slideshow]"]').val() =="true") {
      jQuery('input[name="wpbpp_settings[slideshowSpeed]"]').show();
      alert ('show');
    } else {
       jQuery('input[name="wpbpp_settings[slideshowSpeed]"]').hide();
       alert ('hide');

    }   
   }

1 个答案:

答案 0 :(得分:1)

尝试选择已选中的一个:

jQuery('input[name="wpbpp_settings[slideshow]"]:checked').val()

或点击处理程序内部:

this.value
$(this).val()

http://jsfiddle.net/rW8fH/