【问题标题】:Show / Hide Fields Using Radio Buttons in PHP Based Dynamic Form在基于 PHP 的动态表单中使用单选按钮显示/隐藏字段
【发布时间】:2021-03-16 15:50:29
【问题描述】:

我想针对我的单选按钮的每个选择显示/隐藏我的下拉字段:

HTML 和 PHP

<?php $Sr = 1; $FPSTopics = mysqli_query($con, "SELECT * FROM table");
  for($i=0; $i< $FPSData = mysqli_fetch_array($FPSTopics); $i++){
 ?>
<tr>
<td style="min-width:50px;"><?php echo $Sr; ?></td>
<td><input type="text" value="<?php echo $FPSData['PFS_topic']; ?>"  readonly required="required" size="58" ></td>
 
<input type="hidden" name="FPSTopic[]" value="<?php echo $FPSData['PFS_topic_id']; ?>" />
<td style="min-width:50px;text-align:center; font-weight:bold">
<label>Yes</label>
<input type="radio" name="Suitability[<?php echo $i; ?>]" value="Y" id="YesCheck" /> 
<label>No</label>
<input type="radio" name="Suitability[<?php echo $i; ?>]" value="N" id="NoCheck" /></td>                                                    
                                                   
<td style="min-width:50px; color:blue; font-weight:bold" class="ShowPriority">
<select class="form-control my-1 mr-sm-2" id="Priority[]" name="Priority[]" >
<option selected disabled value="">Choose Priority...</option>
<option value="1">1 - High</option>
<option value="2">2 - Intermediate</option>
<option value="3">3 - Low</option>
</select>
</td>                                                    
</tr>
<?php $Sr++; } ?>

如果从单选按钮中选择“是”。应显示优先领域。如果有人选择否,那么优先级字段应该隐藏。

以下是我的 JS 代码。但在选择时,它会隐藏整列而不是一个字段。

           $('#NoCheck').click(function () {
                      $('.ShowPriority').hide();
                  })
                  $('#YesCheck').click(function () {
                      $('.ShowPriority').show();
                  })

【问题讨论】:

  • ID 在 HTML 文档中必须是唯一的。
  • 另外,下次如果不是 PHP 问题,请删除所有 PHP。单击编辑,然后单击[&lt;&gt;] sn-p 编辑器并提供仅包含相关 HTML、JavaScript 和 CSS 的 minimal reproducible example

标签: javascript php html jquery css


【解决方案1】:

ID 必须是唯一的

您可能会发现这更可靠,因为我相对于行导航

$('[name^=Suitability]').on("click", function() { // any button that starts with Suitablility
  $(this).closest('tr')
    .find('.ShowPriority')
    .toggle(this.value==="Y"); // show if "Y", Hide if not
})
.ShowPriority { display:none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td style="min-width:50px;">
      Row 1
    </td>
    <td><input type="text" value="topic" readonly required="required" size="58"></td>

    <input type="hidden" name="FPSTopic[]" value="topic" />
    <td style="min-width:50px;text-align:center; font-weight:bold">
      <label>Yes</label>
      <input type="radio" name="Suitability1" value="Y" />
      <label>No</label>
      <input type="radio" name="Suitability1" value="N"/>
    </td>
    <td style="min-width:50px; color:blue; font-weight:bold" class="ShowPriority">
      <select class="form-control my-1 mr-sm-2" id="Priority[]" name="Priority[]">
        <option selected disabled value="">Choose Priority...</option>
        <option value="1">1 - High</option>
        <option value="2">2 - Intermediate</option>
        <option value="3">3 - Low</option>
      </select>
    </td>
  </tr>
  <tr>
    <td style="min-width:50px;">
      Row 2
    </td>
    <td><input type="text" value="topic" readonly required="required" size="58"></td>

    <input type="hidden" name="FPSTopic[]" value="topic" />
    <td style="min-width:50px;text-align:center; font-weight:bold">
      <label>Yes</label>
      <input type="radio" name="Suitability2" value="Y" />
      <label>No</label>
      <input type="radio" name="Suitability2" value="N" />
    </td>
    <td style="min-width:50px; color:blue; font-weight:bold" class="ShowPriority">
      <select class="form-control my-1 mr-sm-2" id="Priority[]" name="Priority[]">
        <option selected disabled value="">Choose Priority...</option>
        <option value="1">1 - High</option>
        <option value="2">2 - Intermediate</option>
        <option value="3">3 - Low</option>
      </select>
    </td>
  </tr>
</table>

【讨论】:

  • 你能告诉我如何标记必填字段并在未选择下拉列表中的值时显示错误吗?我被困在那里无法脱身
  • 只需在标签上添加required。并将带有 value="" 的please select 添加到选择
  • $('input[name*=Suitability]').click(function () { if ($(this).is(":checked") && $(this).val() == "Y") { $(this).closest("tr").find(".ShowPriority").show(); //显示 $(this).closest("tr").find(".优先级").attr('required', '').attr('data-error', '这个字段是必填的。'); $(this).closest("tr").find(".Priority") .attr('data-error', error.text()); } else { $(this).closest("tr").find('.ShowPriority').hide(); //隐藏 $(this) .closest("tr").find(".Priority").removeAttr('required', ''); $(this).closest("tr").find(".Priority").removeAttr('data -error', error.text()); } })
  • 我正在使用这个功能。在此函数中,data-error 属性不起作用,如果未选择下拉菜单,则现在显示错误。
【解决方案2】:

您可以使用$(this).val() == "Y" 来查看单选是否被选中并且选中单选的值为Y 然后显示选择否则隐藏它。

演示代码

//on click of radio button
$('input[name*=Suitability]').click(function() {
  //check  value is Y
  if ($(this).val() == "Y") {
    $(this).closest("tr").find(".ShowPriority").show(); //show

  } else {
    $(this).closest("tr").find('.ShowPriority').hide(); //hide
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<table>
  <tr>
    <td style="min-width:50px;">
      1
    </td>
    <td><input type="text" value="<?php echo $FPSData['PFS_topic']; ?>" readonly required="required" size="58"></td>

    <input type="hidden" name="FPSTopic[]" value="<?php echo $FPSData['PFS_topic_id']; ?>" />
    <td style="min-width:50px;text-align:center; font-weight:bold">
      <label>Yes</label>
      <input type="radio" name="Suitability[1]" value="Y" />
      <label>No</label>
      <input type="radio" name="Suitability[1]" value="N" /></td>

    <td style="min-width:50px; color:blue; font-weight:bold" class="ShowPriority">
      <select class="form-control my-1 mr-sm-2" name="Priority[]">
        <option selected disabled value="">Choose Priority...</option>
        <option value="1">1 - High</option>
        <option value="2">2 - Intermediate</option>
        <option value="3">3 - Low</option>
      </select>
    </td>
  </tr>
  <tr>
    <td style="min-width:50px;">
      2
    </td>
    <td><input type="text" value="<?php echo $FPSData['PFS_topic']; ?>" readonly required="required" size="58"></td>

    <input type="hidden" name="FPSTopic[]" value="<?php echo $FPSData['PFS_topic_id']; ?>" />
    <td style="min-width:50px;text-align:center; font-weight:bold">
      <label>Yes</label>
      <input type="radio" name="Suitability[2]" value="Y" />
      <label>No</label>
      <input type="radio" name="Suitability[2]" value="N" /></td>

    <td style="min-width:50px; color:blue; font-weight:bold" class="ShowPriority">
      <select class="form-control my-1 mr-sm-2" name="Priority[]">
        <option selected disabled value="">Choose Priority...</option>
        <option value="1">1 - High</option>
        <option value="2">2 - Intermediate</option>
        <option value="3">3 - Low</option>
      </select>
    </td>
  </tr>
</table>

【讨论】:

  • 谢谢你,伙计,这句话对我来说就像一个魅力。真的很有帮助。
  • 再次感谢您对此事的支持。我面临一个问题。如何在选择“是”时标记需要的优先级选择标签,并在选择“否”时将其删除。我尝试了以下代码,但对我不起作用。
  • $('input[name*=Suitability]').click(function () { //检查radio是否被选中并且值为Y if ($(this).is(":checked ") && $(this).val() == "Y") { $(this).closest("tr").find(".ShowPriority").show(); //显示 $(this).最接近(“tr”).find(“.Priority”).attr('required', ''); } else { $(this).closest("tr").find('.ShowPriority').hide( ); //隐藏 $(this).closest("tr").find(".Priority").removeAttr('required'); } })
猜你喜欢
  • 2014-08-27
  • 2019-03-19
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2021-12-08
  • 2013-10-30
  • 1970-01-01
相关资源
最近更新 更多