doufang8965 2013-09-17 00:59
浏览 42
已采纳

已禁用单选按钮值丢失并传递最后一个隐藏的输入值

In my Wordpress plugin that I'm developing, I'm using the WordPress settings api. In the callback that I use to display the setting which contains the radio buttons I enable them and disable them based on a checkbox value. Anyways, I tried to use a hidden input to store their value even though their disabled but that made the situation worse since it always took the value of the last hidden input which in my case was "4".

if(get_option( 'display_style' ) == 2){
$disabled = 'disabled';
}
$html = '<input type="radio" id="onecolumn" class="cardchecked" name="card_columns" value="1"' . checked( 1, get_option( 'card_columns' ), false ) . $disabled . '/>';  
$html .= '<input type="hidden" name="card_columns" value="1" />';  
$html .= '<label for="onecolumn">One</label>';  

$html .= '<input type="radio" id="twoclumn" class="cardchecked" name="card_columns" value="2"' . checked( 2, get_option( 'card_columns' ), false ) . $disabled . '/>';  
$html .= '<input type="hidden" name="card_columns" value="2" />';  
$html .= '<label for="twoclumn">Two</label>';  

$html .= '<input type="radio" id="threecolumn" class="cardchecked" name="card_columns" value="3"' . checked( 3, get_option( 'card_columns' ), false ) . $disabled . '/>';  
$html .= '<input type="hidden" name="card_columns" value="3" />';  
$html .= '<label for="threecolumn">Three</label>';  

$html .= '<input type="radio" id="fourcolumn" class="cardchecked" name="card_columns" value="4"' . checked( 4, get_option( 'card_columns' ), false ) . $disabled . '/>';  
$html .= '<input type="hidden" name="card_columns" value="4" />';  
$html .= '<label for="fourcolumn">Four</label>';  
echo $html;
  • 写回答

1条回答 默认 最新

  • dongyuruan2957 2013-09-17 01:36
    关注

    The hidden input name can not be the same as the radio input's name because it's value is constant and will override the value of any previous input with the same name.

    if(get_option( 'display_style' ) == 2){
    $disabled = 'disabled';
    }
    $html .= '<input type="hidden" name="card_columns" value="1" />'; 
    $html = '<input type="radio" id="onecolumn" class="cardchecked" name="card_columns" value="1"' . checked( 1, get_option( 'card_columns' ), false ) . $disabled . '/>';    
    $html .= '<label for="onecolumn">One</label>';  
    ...
    

    You will have to set the value of the hidden input field with javascript. Placing just one hidden input at the top of your radio will insure that if the radio button are disabled that the value of the hidden input is submitted, but if your radio buttons are enable they will override the value the hidden input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败