【问题标题】:Hquery radio button always evaluating to trueJquery单选按钮总是评估为真
【发布时间】:2014-05-05 09:45:42
【问题描述】:

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

你能指出我的错误吗?

表格sn-p:

<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');

    }   
   }

【问题讨论】:

    标签: jquery if-statement radio-button


    【解决方案1】:

    尝试选择已选中的:

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

    或者在点击处理程序内部:

    this.value
    $(this).val()
    

    http://jsfiddle.net/rW8fH/

    【讨论】:

      猜你喜欢
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 2016-08-25
      相关资源
      最近更新 更多