【问题标题】:How can I check a radio button before submitting the form with a button?如何在提交带有按钮的表单之前检查单选按钮?
【发布时间】:2020-05-27 23:00:08
【问题描述】:

我有一个带有表格的表格,该表格的每一行都是不同的报价。因此,我在每一行也有一些按钮来下载报价文档或删除报价。该按钮提交表单,然后它们转到另一个执行所需操作的 php 文件。

我的问题是在提交表单之前必须检查所需行的单选按钮。 javascript 有什么方法可以让您在单击这两个按钮之一时,首先自动选择该行的单选按钮,然后提交表单?

<form class="filterSelection" action="../php/editOffer/getInfo.php" method="POST">
    <table class="accountsTable">
        <thead>
            <tr>
                <th>Selected</th>
                <th>Project ID</th>
                <th>Revision</th>
                <th>Project Description</th>
                <th>Customer</th>
                <th>Date</th>
                <th>Creator</th>
                <th>Documentation</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="radio" name="selectedOffer" id="selectedOffer" required="" value="1-1"></td>
                <td>1</td>
                <td>1</td>
                <td>Test</td>
                <td>Info</td>
                <td>2020-02-10</td>
                <td>Name</td>
                <td>
                    <button type="submit" class="download" name="action" value="Download"><i class="fa fa-download" aria-hidden="true"></i></button>
                </td>
                <td>
                    <button type="submit" class="delete" name="action" value="Delete" onclick="return confirm('Do you want to delete the selected offer? This action can´t be undone')">
                        <i class="far fa-trash-alt"></i>
                    </button>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <button name="action" class="nextButton" type="submit" value="Next"><i class="fas fa-arrow-alt-circle-right fa-2x"></i></button>
</form>

【问题讨论】:

  • 当然有。你试过什么?
  • 在每一行都有单选按钮,然后在底部有一个单独的下载/删除按钮,它作用于单选按钮中选择的项目不是更有意义吗?要么,要么完全摆脱单选按钮,并在行中有单独的表格,并带有一个记录选择的隐藏字段。现在,您似乎将两种不同的 UI 模式混为一谈。
  • @ADyson 因为我需要提交单选按钮的值。对不起,但我是新手,我可以用隐藏字段来做吗?如果可能的话,它是如何工作的?
  • 抱歉,不确定您要回复哪些 cmet。我添加了几个,但后来我重新考虑了所有这些 cmets 并删除了它们,只添加了一个更简单的。正如我在上面剩下的一条评论中指出的那样,通常有两种方法可以处理这种设计。其中之一保留了您的单选选项,但减少了按钮的数量。另一个保留按钮并用隐藏字段替换单选选项。
  • @d1649356 当然,我看不出你到底做了什么,但从你描述的内容来看,我猜你可能忘记了我所说的对于这种方法必须是每行单独的&lt;form&gt;&lt;/form&gt; 标签?因此,每个单独的表单只会包含一个隐藏字段。

标签: javascript php html button radio-button


【解决方案1】:

最后我解决了添加隐藏输入和删除单选按钮的问题。我也改变了表格的结构。现在,这是我的有效代码:

<table class = "accountsTable">
            <thead>
                <tr>
                    <th>Project ID</th>
                    <th>Revision</th>
                    <th>Project Description</th>
                    <th>Customer</th>
                    <th>Date</th>
                    <th>Creator</th>
                    <th>Download</th>
                    <th>Delete</th>
                    <th>Edit</th>
                </tr>
            </thead>
            <tbody>
              <tr>
                    <form class="filterSelection" action="../php/editOffer/getInfo.php" method="POST">
                        <td>1</td>
                        <td>1</td>
                        <td>Test</td>
                        <td>Info</td>
                        <td>2020-02-10</td>
                        <td>Name</td>
                        <td><button type="submit" class="download" name="action" value="Download"><i class="fa fa-download" aria-hidden="true"></i></button>
                            </td>
                        <td><button type="submit" class="delete" name="action" value="Delete" 
                                    onclick="return confirm('Do you want to delete the selected offer? This action can´t be undone')">
                                <i class="far fa-trash-alt"></i></button></td>
                        <td><button name="action" class="edit" type="submit" value="Next"><i class="fas fa-edit"></i></button></td>
                        <input type="hidden" name="selectedOffer" value="1-1"/>
                    </form>
                </tr>
                                        <tr>
                    <form class="filterSelection" action="../php/editOffer/getInfo.php" method="POST">
                        <td>1</td>
                        <td>2</td>
                        <td>Demo</td>
                        <td>Info1</td>
                        <td>2020-02-13</td>
                        <td>Name1</td>
                        <td></td>
                        <td><button type="submit" class="delete" name="action" value="Delete" 
                                    onclick="return confirm('Do you want to delete the selected offer? This action can´t be undone')">
                                <i class="far fa-trash-alt"></i></button></td>
                        <td><button name="action" class="edit" type="submit" value="Next"><i class="fas fa-edit"></i></button></td>
                        <input type="hidden" name="selectedOffer" value="1-2"/>
                    </form>
                </tr>

【讨论】:

    【解决方案2】:

    您需要在表单中进行一些小的更改,如下所示:

    <form class="filterSelection" id="filter-form" action="../php/editOffer/getInfo.php" method="POST">
        <table class="accountsTable">
            <thead>
                <tr>
                    <th>Selected</th>
                    <th>Project ID</th>
                    <th>Revision</th>
                    <th>Project Description</th>
                    <th>Customer</th>
                    <th>Date</th>
                    <th>Creator</th>
                    <th>Documentation</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><input type="radio" name="selectedOffer" id="selectedOffer-1" value="1-1"></td>
                    <td>1</td>
                    <td>1</td>
                    <td>Test</td>
                    <td>Info</td>
                    <td>2020-02-10</td>
                    <td>Name</td>
                    <td><button type="button" name="action" data-row="1" onclick="btnAction(this)" value="Download"><i
                                class="fa fa-download" data-row="1" aria-hidden="true"></i> Download</button>
                    </td>
                    <td><button type="button" name="action" onclick="btnAction(this)" value="Delete">
                            <i class="far fa-trash-alt"></i> Delete</button></td>
                </tr>
            </tbody>
        </table><br>
        <button name="submit" class="nextButton" type="button" value="Next"><i
                class="fas fa-arrow-alt-circle-right fa-2x"></i> Next</button>
    </form>
    

    现在这里是检查一些验证的 javascript 代码:

    <script>
        function btnAction(btn) {
            var row = btn.dataset.row;
            var radio = document.getElementById("selectedOffer-"+row);
            radio.checked = true;
            document.getElementById("filter-form").submit();
        }
    </script>
    

    希望这对你有用

    【讨论】:

    • 我已经编辑了答案可能对这个问题有用
    • 谢谢,但您仍然错过了不应要求用户选择选项的要点。它应该自动选择离被点击按钮最近的单选按钮。更仔细地阅读问题。另外,请从您的文本中删除所有对 jQuery 的引用。最后,最后的代码和文本似乎顺序错误。
    • 是的,你可以在代码中看到的函数btnAction(btn)会自动选择最近的收音机,第二个函数checkIsSelected()将扫描所有带有selectedOffer名称的收音机,并检查其中是否有人被选中与否
    • 但是在那种情况下,checkIsSelected() 没有任何意义,对吧?如果代码只是要选择它,您不必检查任何内容或提示用户。这就是为什么我很困惑。我根本不明白你为什么需要 checkIsSelected 函数。
    • 如果您可以看到底部有下一个按钮,因此如果用户直接单击下一个按钮而不选择单选,则不应提交表单,否则如果您想在下一个按钮上选择所有单选,那么您可以简单地在 forEach 中添加 element.checked=true 并从函数 checkIsSelected() 中删除所有条件
    【解决方案3】:

    您可以定位最近的 tr 以找到特定的单选按钮。然后使用setAttribute()设置checked属性。

    您可以尝试以下方法:

    var buttons = document.querySelectorAll('button[type=submit');
    buttons.forEach(function(btn){
      btn.addEventListener('click', function(el, e){
        this.closest('tr').querySelector('[type=radio]').setAttribute('checked', 'checked');
        alert(this.closest('tr').querySelector('[type=radio]').getAttribute('checked'));
      });
    })
    <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    <form class = "filterSelection" action = "../php/editOffer/getInfo.php" method = "POST">
      <table class = "accountsTable">
          <thead>
              <tr>
                  <th>Selected</th>
                  <th>Project ID</th>
                  <th>Revision</th>
                  <th>Project Description</th>
                  <th>Customer</th>
                  <th>Date</th>
                  <th>Creator</th>
                  <th>Documentation</th>
                  <th>Delete</th>
              </tr>
          </thead>
          <tbody>
             <tr>
                <td><input type="radio" name="selectedOffer" id="selectedOffer" value="1-1">
                </td>
                 <td>1</td>
                 <td>1</td>
                 <td>Test</td>
                 <td>Info</td>
                 <td>2020-02-10</td>
                 <td>Name</td>
                 <td><button type="submit" class="download" name="action" value="Download"><i class="fa fa-download" aria-hidden="true"></i></button>
                  </td>
                 <td><button type="submit" class="delete" name="action" value="Delete" onclick="return confirm('Do you want to delete the selected offer? This action can´t be undone')"><i class="fa fa-trash"></i></button></td>
            </tr>
            
            <tr>
                <td><input type="radio" name="selectedOffer" id="selectedOffer" value="1-1">
                </td>
                 <td>1</td>
                 <td>1</td>
                 <td>Test</td>
                 <td>Info</td>
                 <td>2020-02-10</td>
                 <td>Name</td>
                 <td><button type="submit" class="download" name="action" value="Download"><i class="fa fa-download" aria-hidden="true"></i></button>
                  </td>
                 <td><button type="submit" class="delete" name="action" value="Delete" onclick="return confirm('Do you want to delete the selected offer? This action can´t be undone')"><i class="fa fa-trash"></i></button></td>
            </tr>
        </tbody>
      </table><br>
      <button name="action" class="nextButton" type="submit" value="Next"><i class="fa fa-arrow-alt-circle-right fa-2x"></i>    </button>
    </form>

    【讨论】:

    • 你错过了它不应该要求用户选择一个选项的观点。它应该自动选择离被点击按钮最近的单选按钮。仔细阅读问题。
    【解决方案4】:

    为按钮添加点击事件

    <td>
        <button type="button" class="delete" name="action" value="Delete" onclick="someAction(this);">
            <i class="far fa-trash-alt"></i>
        </button>
    </td>
    

    jQuery 代码

    在页面中添加 Jquery 库

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    

    使用下面的脚本

    function someAction(ds) {
    
       if(!$(ds).closest('tr').find('input[type="radio"]').is(':checked')){         
    
         $(ds).closest('tr').find('input[type="radio"]').prop('checked',true);
         if(confirm('Do you want to delete the selected offer? This action can´t be undone')){
              $(ds).parents('form').submit();
         }
      }               
    }
    

    【讨论】:

    • 这不是我想要的。我希望当您单击按钮时,自动选择单选按钮
    • if(!$(ds).closest('tr').find('input[type="radio"]').is(':checked')){ $(ds) .closest('tr').find('input[type="radio"]').prop('checked',true); if(confirm('你想删除选中的offer吗?这个动作不能撤销')){ $(ds).parents('form').submit(); } }
    • 使用此脚本自动检查未选中的单选按钮并提交表单
    • 当 OP 不使用它并且没有要求它时,为什么每个人都继续推荐 jQuery。这是这样做的第三个(包括一个已删除的)答案。在 jQuery 中没有什么是使用原生 JavaScript 无法完成的。没有必要仅仅为了一个小功能就将 jQuery 的开销添加到站点中。无论如何,如果您想更新答案,请将更新后的代码添加到答案本身(使用答案的“编辑”按钮),而不是 cmets。
    猜你喜欢
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    相关资源
    最近更新 更多