简体   繁体   English

在提交带有按钮的表单之前,如何检查单选按钮?

[英]How can I check a radio button before submitting the form with a button?

I have a form with a table and each row of that table is a different offer.我有一个带有表格的表格,该表格的每一行都是不同的报价。 So, I also have some buttons in each row to download the documentation of the offer or delete the offer.因此,我在每一行中也有一些按钮,用于下载报价的文档或删除报价。 That buttons submit the form and they go to another php file that does the desired action.该按钮将表单和它们 go 提交到另一个执行所需操作的 php 文件。

My problem is that the radio button of the desired row has to be checked before I submit the form.我的问题是在提交表单之前必须选中所需行的单选按钮。 Is there any way with javascript that when you click one of those two button, first it selects automatically the radio button of that row and then submits the form? 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>

You can target the closest tr to find the specific radio button.您可以定位最近的 tr 来查找特定的单选按钮。 Then set the checked attribute using setAttribute() .然后使用setAttribute()设置选中的属性。

You can try the following way:您可以尝试以下方式:

 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>

You need to make some small changes in you form like below:您需要对表单进行一些小的更改,如下所示:

<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>

Now here is the javascript code to check some validation:现在这里是 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>

Hope this will work for you solution希望这对您有用

Finally I solved adding a hidden input and deleting the radio buttons.最后我解决了添加隐藏输入和删除单选按钮的问题。 I have also change the structure of the forms. Now, this is my code that works:我还更改了 forms 的结构。现在,这是我的代码:

<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>

add on click event to the button给按钮添加点击事件

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

Jquery Code Jquery 代码

Add Jquery library to the page页面添加Jquery库

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

Use below 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();
     }
  }               
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM