【问题标题】:Dynamic list of radio buttons - how do I retrieve value instead of retrieve BY VALUE单选按钮的动态列表 - 我如何检索值而不是检索 BY VALUE
【发布时间】:2018-02-27 15:20:16
【问题描述】:

我有一个从数据库加载的单选按钮列表。下面的 $missing 是一个 mysql 查询结果,我在其中检索单选按钮的标签。这部分工作正常:

<div id="container">
<div id="col1">
    <div>
        <div id="dam_return">
        <form method="post" action="#" >';
        foreach ($missing as &$path) {
            $prov = $path['template_name'];
            $return .= '<input type="radio" name="radio_group1" value="$prov" class="radios">'.$prov."</option>";
            $return .= '<br>';
        }   
    $return .= '</form>';

我将上面的这个返回值注入到我的 PHP 代码中其他地方的 HTML 中(是的,我在之后正确地关闭了 div 等等)。到目前为止,这部分工作正常。

那么,我的单选按钮类有一个监听器:

$(".radios").click(function () {
        $('[id$=mytextbox]').val("asdadasd");
        $.get('?page=EntitlementTemplates&action=dothething&var1=somestuff', function() {
                $(node).removeClass('selected');
                $(node).addClass('delete');
            });

    //location.reload();
});

现在我正在阅读我的文章:

 if (isset($_GET['action'])) {
            switch (strtolower($_GET['action'])) {

                case 'dothething' :
                //I want to manipulate/identify the selected radio button
                break;

在这个获取代码中,我想获取按下的单选按钮的值。网上有很多使用 switch 语句来获取离散值列表的示例......但我想检索单选按钮 BY 值。我将使用此单选按钮值然后将一些新数据加载到页面...但是现在我不知道如何获取选择了哪个单选按钮?对于这种情况,我似乎找不到任何东西。

【问题讨论】:

    标签: php jquery post get


    【解决方案1】:

    为了在您的点击事件处理程序中将点击的元素值传递给您的服务器,您必须在 GET 请求查询中将其作为参数传递。

    在您的点击处理程序中:

    $(".radios").click(
            $('[id$=mytextbox]').val("asdadasd");
            var radioValue = $(this).val(); 
            $.get('?page=EntitlementTemplates&action=dothething&var1=somestuff&clickedRadioValue='+radioValue , function() {
                    $(node).removeClass('selected');
                    $(node).addClass('delete');
                });
    
        //location.reload();
    });
    

    在你的 php 脚本中:

    if (isset($_GET['action'])) {
                switch (strtolower($_GET['action'])) {
                    case 'dothething' :
                    $clickedRadioValue = $_GET['clickedRadioValue'];
                    //further manipulations
                    break;
    
                   //other cases
                }
    }
    

    【讨论】:

      【解决方案2】:

      选中的值会以电台组名下发;在这种情况下:$_POST['radio_group1'] ... 由于表单上指定的方法,它位于 $_POST 而不是 $_GET 下。

      【讨论】:

      • 哦?我可以将&lt;form method="post" action="#" &gt; 更改为&lt;form method="get" action="#" &gt; 然后 $_GET['radio_group1'] 将包含一个我可以操作的值(并且可以在'dothething'块中进行操作?)
      • 是的...但是值将在查询字符串中传递并成为 URL 的可读部分。如果你对此没问题,那很好。为什么不想使用$_POST?我觉得它看起来更干净。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 2020-11-22
      • 2018-07-23
      • 2012-04-20
      • 1970-01-01
      相关资源
      最近更新 更多