【问题标题】:If else statement php/js/jquery (Radio buttons and textfields)If else 语句 php/js/jquery(单选按钮和文本字段)
【发布时间】:2016-03-12 17:09:38
【问题描述】:

我使用的代码如下:https://jsfiddle.net/t6noeL9q/1/

我想在 php 中使用 if else 语句。例如,如果我选择单选按钮 1 并输入严重程度,则建议应显示在建议 1 文本字段中。建议应根据概率和严重程度的不同而改变。我该怎么做?

代码:

<div class="radio">

  <tr>
    <td>
      <input class="form-control" style="background:white; border-style:none; box-shadow: inset 0px 0px 0px 0px; " type="text" name="q1" readonly='true' value="No security of hardware" />
    </td>

<p>
</p>
    <td>
      Rare &nbsp;<input style="margin-left:-2px;" type="radio" name="rad1[]" value="Rare">
    </td>
    <td>
      Unlikely &nbsp;<input style="margin-left:-2px;" type="radio" name="rad1[]" value="Unlikely">
    </td>
    <td>
      Moderate &nbsp;<input style="margin-left:-2px;" type="radio" name="rad1[]" value="Moderate">
    </td>
    <td>
      Likely &nbsp;<input style="margin-left:-2px;" type="radio" name="rad1[]" value="Likely">
    </td>
    <td>
      Very Likely &nbsp;<input style="margin-left:-2px;" type="radio" name="rad1[]" value="Very Likely">
    </td>
    <br>
    <td>
      Severity(Rate 1-10)&nbsp;<input style="width:50px;" class="form-control" type="text" name="severity1" />
    </td>
</div>
</tr>

<br>


<tr>
  <div class="radio">
    <tr>
      <td>
      <br>
        <input class="form-control" style="background:white; border-style:none; box-shadow: inset 0px 0px 0px 0px; " type="text" name="q2" readonly='true' value="No security in access control to systems" />
      </td>

      <td>
        Rare &nbsp;<input style="margin-left:-2px;" type="radio" name="rad2[]" value="Rare">
      </td>
      <td>
        Unlikely &nbsp;<input style="margin-left:-2px;" type="radio" name="rad2[]" value="Unlikely">
      </td>
      <td>
        Moderate &nbsp;<input style="margin-left:-2px;" type="radio" name="rad2[]" value="Moderate">
      </td>
      <td>
        Likely &nbsp;<input style="margin-left:-2px;" type="radio" name="rad2[]" value="Likely">
      </td>
      <td>
        Very Likely &nbsp;<input style="margin-left:-2px;" type="radio" name="rad2[]" value="Very Likely">
      </td>
      <td>
        Severity(Rate 1-10)&nbsp;<input style="width:50px;" class="form-control" type="text" name="severity2" />
      </td>

  </div>
  </tr>

  <div class="radio">
    <tr>
      <td>
      <br>
      <br>
Recommendation1: <input type="text" name="r1"/> <br><br>
Recommendation2: <input type="text" name="r2"/>

【问题讨论】:

  • 您不能在客户端模式(http 浏览器)下使用 php。为此,请使用 js/jQuery。 php 是一种服务器端语言:它不直接与浏览器操作交互。
  • 我该怎么做?请在 jsfiddle 中找到上述代码的链接。

标签: javascript jquery if-statement radio-button textfield


【解决方案1】:

如果if 语句必须在 PHP 中,您可以创建一个表单并将其传递给一个新的 PHP 脚本,或者(更可取的方式)您可以使用 AJAX 和 JQUERY 将单选按钮的状态传递给PHP 脚本并检索其输出。第二种解决方案允许您在不重新加载页面的情况下显示结果。 但是,这不是您应该这样做的方式。解决这个问题的正确方法是在 javascript 中编写一个函数来检查单选按钮的状态并动态显示结果,而不是使用 PHP。我编写了一个简单的代码 sn-p 来向您展示如何做到这一点(我试图通过提供一个更有可能解决您的实际问题的解决方案来避免 XY 问题)。

当然,如果你需要做一些服务器端计算,那么我描述的 AJAX / POST 方法是不可避免的。

$(document).ready(function(){
  function display_result(radio_value, text_value){
    switch(radio_value){
      case "Rare":{
        /*do stuff*/
        break;
      }
      case "Unlikely":{
        /*do some other stuff*/
        break;
      }
      /* etc. */
    }
    alert(radio_value + " " + text_value); //Just to show you that it's working
  }
  $("#row1 input[type=radio]").click(function(){
    display_result($(this).val(), $("#row1 [name=severity1]").val());
  });
  $("#row1 [name=severity1]").change(function(){
    display_result($("#row1 input[type=radio]:checked").val(), $(this).val());
  });
  $("#row2 input[type=radio]").click(function(){
    display_result($(this).val(), $("#row2 [name=severity2]").val());
  });
  $("#row2 [name=severity2]").change(function(){
    display_result($("#row2 input[type=radio]:checked").val(), $(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="radio" id="row1">

  <tr>
    <td>
      <h5><b>Physical Security</b></h5></td>
  </tr>
  <tr>
    <td>
      <input class="form-control" style="background:white; border-style:none; box-shadow: inset 0px 0px 0px 0px; " type="text" name="q1" readonly='true' value="No security of computer equipment" />
    </td>


    <td>
      <input style="margin-left:-2px;" type="radio" name="rad1[]" value="Rare">
    </td>
    <td>
      <input style="margin-left:-2px;" type="radio" name="rad1[]" value="Unlikely">
    </td>
    <td>
      <input style="margin-left:-2px;" type="radio" name="rad1[]" value="Moderate">
    </td>
    <td>
      <input style="margin-left:-2px;" type="radio" name="rad1[]" value="Likely">
    </td>
    <td>
      <input style="margin-left:-2px;" type="radio" name="rad1[]" value="Very Likely">
    </td>
    <td>
      <input style="width:50px;" class="form-control" type="text" name="severity1" />
    </td>
</div>
</tr>



<tr>
  <div class="radio" id="row2">
    <tr>
      <td>
        <input class="form-control" style="background:white; border-style:none; box-shadow: inset 0px 0px 0px 0px; " type="text" name="q2" readonly='true' value="No security in access control to systems" />
      </td>

      <td>
        <input style="margin-left:-2px;" type="radio" name="rad2[]" value="Rare">
      </td>
      <td>
        <input style="margin-left:-2px;" type="radio" name="rad2[]" value="Unlikely">
      </td>
      <td>
        <input style="margin-left:-2px;" type="radio" name="rad2[]" value="Moderate">
      </td>
      <td>
        <input style="margin-left:-2px;" type="radio" name="rad2[]" value="Likely">
      </td>
      <td>
        <input style="margin-left:-2px;" type="radio" name="rad2[]" value="Very Likely">
      </td>
      <td>
        <input style="width:50px;" class="form-control" type="text" name="severity2" />
      </td>

  </div>
  </tr>

  <div class="radio">
    <tr>
      <td>

【讨论】:

  • 我不必显示消息框。我需要根据概率和严重性显示适当的建议。
  • 正如我在评论中所说 - 消息框只是为了向您显示该函数正在接收正确的数据。你可以对 switch 语句中的数据做任何你想做的事情。你想让我写剩下的代码,它会根据输入(概率和严重性)填充一个文本字段吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2022-01-25
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
相关资源
最近更新 更多