如何在回发时重置单选按钮选择?

发布于 2024-09-11 13:54:34 字数 226 浏览 13 评论 0原文

这是一个 Drupal 表单问题。

我正在开发一个 UI,它根据选定的日期动态创建 3 个单选按钮行。我遇到的问题是,如果我对单选按钮进行一些选择并选择另一个日期来获取另一组单选按钮,第一个列表中的单选按钮将保持其选定状态并忽略“#default_value”属性。

我想要的是,当选择新日期时,所有动态创建的无线电都将被取消选择。我现在能想到的唯一方法是创建一些唯一的密钥,用于每组收音机的收音机名称中。

This is a Drupal Forms question.

I am working on a UI that dynamically creates rows of 3 radio buttons based on a selected date. The problem I am having is that if I make some selections of the radio buttons and choose another date to get another set of radio buttons, the radios that were in the first list maintain their selected status and ignore the '#default_value' attribute.

What I want is when a new date is selected is that all the dynamically created radios to be unselected. The only way i can think of now is to create some unique key to use in the radios name for each set of radios.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温馨耳语 2024-09-18 13:54:34

这是创建单选按钮行的代码的一部分。对于 $result 中的每一行,我需要创建一行 4 个单选按钮。我还尝试将单选按钮的 #default_value 设置为之前选择的值。这对于初始提交来说很好,但如果我进行更改,并且不保存,并且想要获取原始选定的值,它会保留当前选定的值。

while ($my_data = db_fetch_object($result))
{   
  $form['opts'][$my_data->opt_id] = array(
    '#type' => 'radios',
    '#title' => t($my_data->option_name),
        '#options' => array(
         0 => 'N/A',
         1 => 'Yes', 
         2 => 'No',
         3 => 'Sometimes',
         ),
   );

   $form['opts'][$my_data->opt_id]['#default_value'] =  0;
   if($my_data->selected_opt_id != 0){
     $form['opts'][$my_data->opt_id]['#default_value'] =  $my_data->selected_opt_id;
   }
}

我最终所做的是使用 JavaScript 在每次请求列表时设置默认值。我认为问题在于“#default_value”仅适用于控制表单的初始视图,并且不适用于回发。任何人都可以确认吗?

here is part of the code that creates the rows of radio buttons. For each row in $result, I need to create a row of 4 radio buttons. I also try to set the #default_value of the radio buttons to what ever value was previous selected. This is fine for the initial submit, but if i make changes, and don't save, and want to get the original selected values, it maintains the currently selected values.

while ($my_data = db_fetch_object($result))
{   
  $form['opts'][$my_data->opt_id] = array(
    '#type' => 'radios',
    '#title' => t($my_data->option_name),
        '#options' => array(
         0 => 'N/A',
         1 => 'Yes', 
         2 => 'No',
         3 => 'Sometimes',
         ),
   );

   $form['opts'][$my_data->opt_id]['#default_value'] =  0;
   if($my_data->selected_opt_id != 0){
     $form['opts'][$my_data->opt_id]['#default_value'] =  $my_data->selected_opt_id;
   }
}

What I ended up doing was using JavaScript to set the default values every time the listing is requested. I think the issue was that "#default_value" is only good for controlling the initial view of the form, and isn't used on postback. Anyone can confirm?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文