javascript - 当用户选择 False Hit 单选按钮时,如果文本字段为空,如何确保无法提交表单?

标签 javascript html

我正在做一些事情来阻止用户提交 HTML 表单而不留下错误点击的评论。

条件:

  • 仅当选择“False Hit”单选按钮时
  • 备注文本字段不能为空(必填)

目前,我使用 JavaScript 来检查文本字段(备注)是否为空。

function empty() {
var x;
x = document.getElementById("roll-input").value;
if (x == "") {
    alert("Please enter remarks for false hits selected.");
    return false;
};

我还可以使用 JavaScript 检查选择了哪个单选按钮。

<input type="radio" name="select" id="truehit" value="true" />
<input type="radio" name="select" id="falsehit" value="false" />

if(document.getElementById('falsehit').checked) {
  //False Hit radio button is checked
}
else if(document.getElementById('truehit').checked) {
  //True Hit radio button is checked
}

但是,我一直坚持将这两个条件联系在一起。有没有更简单的方法?任何帮助是极大的赞赏。预先感谢您。

这就是我的表单的样子。

    <body bgcolor="#FFFFFF">
    <br />

 <div align="center" class="h1">
 <h1>Entity Search</h1><br />   
 </div>

 </html>
 <br />

 <?php  
    include("db_connect.php"); //connect to database
    @$loginid = strtoupper(base64_decode($_POST['LanID'])); //to capture 
the user's LanID as the UserID
    if($loginid == "")
    {
        $userID = $_SESSION['loginid'];
    }
    else
    {
        $_SESSION['loginid'] = $loginid;
        $userID = $_SESSION['loginid'];
}
$userID = $_SESSION['loginid'];

//to insert date and time of the records
date_default_timezone_set('Asia/Singapore');
$Date = date('Y-m-d');
$Time = date ('h:i:s a');

$userID = $_SESSION['loginid'];
$loginid = "";

$entityfromform = $_GET["entity"];
?>

<br />

<?php
//connection to database
$connectionInfo = array( "Database"=>"test","UID"=>$test,"PWD"=>$test);
$db_connect=sqlsrv_connect($serverName, $connectionInfo);

//to retrieve the department name from the user database and insert 
department name of the user into the records
    $namesql = "SELECT Name, DeptName, Role FROM RPDB.dbo.Userdb WHERE 
   LanID='$userID';";
$name_query = sqlsrv_query($db_connect,$namesql);

while($row = sqlsrv_fetch_array($name_query, SQLSRV_FETCH_ASSOC))
{
    $nameontop = iconv("gb2312","UTF-8",$row['Name']);
    $admincheck = iconv("gb2312","UTF-8",$row["Role"]);
    $deptnamecheck = iconv("gb2312","UTF-8",$row["DeptName"]);
}

$negative = "Negative Hit: No results found.<br />";
$result = "$entityfromform <br />Negative Hit: No results found.<br />"; 
//variable to insert into the table as result

//search for keyword from form in the Entity Table List where Name = 
keyword
$entitysql = "SELECT * FROM RPDB.dbo.entitysearch_table WHERE Name LIKE 
'%" . $entityfromform . "%' OR Name = '".$entityfromform."'";
$entity_query = sqlsrv_query($db_connect,$entitysql);

//variables
$i=0;
while($row = sqlsrv_fetch_array($entity_query, SQLSRV_FETCH_ASSOC))
{           
    $name[$i] = $row["Name"];
    $brn[$i] = $row["Business_Registration_Number"];
    $address[$i] = $row["Registered_Address"];
$group[$i] = $row["Applicable_Limb_of_Related_Party_Group"];
$selectradio[$i] = 'selectradio'.$i;
$text[$i] = 'text'.$i;
$i++;
}   
      if(@$name != NULL) //Positive Result
    {
        echo "<input type='button' name=uncheck' value='Uncheck All' 
class='uncheck' onClick='window.location.reload()'>"; //uncheck all 
function where it clears all changes
        echo "<h2><div align='center'>Results found for: 
$entityfromform</div></h2><br />";
         //table to display the data
             echo "<div align='center'>
            <table> 
            <tr>
            <th>Name</th>
            <th>Business Registration Number</th>
            <th>Registered Address</th>
            <th>Applicable Limb of Related Party Group</th>
            <th>True Hit</th>
            <th>False Hit</th>
            <th>Unable To Ascertain</th>
            <th>Remarks, for false hit</th>
        </tr>
    </div>";
    echo "<strong><div class='wording'>Positive Hit: </div></strong>";
    echo "<br /><br />";
    $x=0;
    ?>
    <!--The form data is sent for processing to action_handler.php by HTTP POST method-->
    <form method="post" id="form" action="action_handler.php" enctype="multipart/form-data">
    <?php
    //data displayed in tables along with checkboxes for true hit, false hit, unable to ascertain and text box for remarks where applicable
    for($x=0;$x<$i;$x++)
    {
        echo "<tr>
        <td>" . $name[$x] . "</td>
        <td>". $brn[$x] . "</td>
        <td>". $address[$x] . "</td>
        <td>". $group[$x] . "</td>
        
        <td>
        <label class='container'>
        <input type='radio'   id='truehit' name='".$selectradio[$x]."' value='true hit' class='btn' required>
        <span class='checkmark'></span>
        </label>
        </td>
        
        <td>
        <label class='container'>
        <input type='radio'  id='falsehit' name='".$selectradio[$x]."' value='false hit' class='btn'>
        <span class='checkmark'></span>
        </label>
        </td>
        
        <td>
        <label class='container'>
        <input type='radio'  id='unabletoascertain' name='".$selectradio[$x]."' value='unabletoascertain' class='btn'>
        <span class='checkmark'></span>
        </label>
        </td>
        
        <td>
        <label><input type='text' name='".$text[$x]."'> 
        </label>
        </td>
        
        </tr>" ;
    }
    echo "</table>"; //table closed
    echo "<br /><br /><br />";
    echo "<input type='button' value='Submit' class='submit' onClick='empty()'>"; //submit button to confirm
    ?>
    </form>
    <?php
    //sessions to be used in others
    $_SESSION['num'] = $x;
    $_SESSION['entityfromform'] = $entityfromform;
}
else 
{
    echo "<div align='center'><h2>Results found for: $entityfromform</h2> 
   <br />";
        echo "<div class='wording' align='center'>$negative</div><br><br> 
   <br>";
            
        }
    ?>

</table>
</body>

This is how it looks like. I have blurred out the private and confidential data

Inspect element

最佳答案

如果文本框有值,则您的empty()函数不会返回任何内容,因此对于该条件它是未定义的;您可以根据您的要求创建一个函数,例如:

function doSubmit() 
{
   if(document.getElementById('falsehit').checked)
   {
      if(document.getElementById("roll-input").value == '')
      {
         alert("Please enter remarks for false hits selected.");
         return false;
      }
   }
return true;
}

并调用此方法进行验证,因此如果falsehit检查并且remarks有一个值,它将返回truefalse

希望这有帮助;如果我无法理解您的问题,请告诉我..

祝你编码愉快...

关于javascript - 当用户选择 False Hit 单选按钮时,如果文本字段为空,如何确保无法提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55113788/

相关文章:

javascript - 为什么 React fetch 会忽略我正在获取的 JSON 数据?

jquery - 单击图像时在 Colorbox 中加载 iframe

javascript - 简单的 Web 服务器示例 - 为什么我必须将属性名称放在引号中?

javascript - 再次刷新发布值,导致再次进入数据库

javascript - 将 http 对象映射到模型

javascript - 当鼠标悬停时,Chartjs 显示标签和单位统计

html - Bootstrap 3 - 网格未正确对齐

html - 相同样式的 anchor 元素和输入元素的填充差异

html - Ionic - 如何将 ionic 列表变成网格

java - 如何在java程序中从网站获取HTML代码?