Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript forloop onclick显示文本字段_Javascript_Php_Html - Fatal编程技术网

Javascript forloop onclick显示文本字段

Javascript forloop onclick显示文本字段,javascript,php,html,Javascript,Php,Html,控制器 for($x = 1; $x <= $qty; $x++){ $data .= '<b style="margin-left:10px;">Person ' . $x . '</b>'; $data .= '<div class="form-group" style="padding-top:10px;">'; $data .= ' <label for="input-for-age" class="col-sm-

控制器

for($x = 1; $x <= $qty; $x++){
    $data .= '<b style="margin-left:10px;">Person ' . $x . '</b>';
    $data .= '<div class="form-group" style="padding-top:10px;">';
    $data .= '  <label for="input-for-age" class="col-sm-2 text-right control-label" style="padding-top:5px;">Age <span class="required">*</span></label>';
    $data .= '  Age: <input type="checkbox" id="myCheck" name="AgeCheck" onclick="showCheckbox()">';
    $data .= '  <p id="text" style="display:none; color:red;">Check Enabled</b><br><input type="number" placeholder="Enter Number"></p><br>';
}
当单击复选框将启用时,如何使其生效

现在如果有3个复选框,我点击1个复选框就会显示出来 textfield,但如果单击循环中的第二个复选框,则什么也没有 出现

PHP

JS


在这里,我向复选框和文本输入添加了一个自定义属性。使用textid属性,您可以将复选框与匹配的文本输入连接起来。

在这里,您将拥有多个名为myCheck的id,这是不允许的

for($x = 1; $x <= $qty; $x++){
    $data .= '<b style="margin-left:10px;">Person ' . $x . '</b>';
    $data .= '<div class="form-group" style="padding-top:10px;">';
    $data .= '  <label for="input-for-age" class="col-sm-2 text-right control-label" style="padding-top:5px;">Age <span class="required">*</span></label>';
    $data .= '  Age: <input type="checkbox" id="myCheck" name="AgeCheck" onclick="showCheckbox()">';
    $data .= '  <p id="text" style="display:none; color:red;">Check Enabled</b><br><input type="number" placeholder="Enter Number"></p><br>';
}

请发布所有代码,我无法在每页的allOne唯一HTML id属性中看到您的php代码。@MaxMuster在中添加了我的forloop代码question@StackSlave你说每页id是什么意思?我没有看到任何复选框???非常感谢,我想问为什么要添加x使其工作?我还有一个问题,比如说:如果我没有检查其中的一个,如何禁用它?不允许它发送数据@greenboxgoolu id在脚本中必须是唯一的,因此实际上您的循环创建的html代码中id myCheck不是唯一的。为此,您必须创建一个范围:myCheck1、myCheck2。。。等等,这是连接一个变量创建的。
$data .= '  Age: <input type="checkbox" class="myCheck" name="AgeCheck" textid="'.$x.'" onclick="showCheckbox(this)">';
        $data .= '  <p id="text" style="display:none; color:red;">Check Enabled</b><br>
                    <input type="number" placeholder="Enter Number" textid="'.$x.'" class="num-text"/>
                    </p><br>';
function showCheckbox(ele) {
  // Get the output text
  var texts = document.getElementsByClassName("num-text");

  // If the checkbox is checked, display the output text
for(var i =0; i <texts.length; i++){
   var text = texts[i];
  if (ele.checked == true && text.getAttribute("textid") == ele.getAttribute("textid")){
    text.style.display = "block";
  } else {
    text.style.display = "none";
  }
}
}
for($x = 1; $x <= $qty; $x++){
    $data .= '<b style="margin-left:10px;">Person ' . $x . '</b>';
    $data .= '<div class="form-group" style="padding-top:10px;">';
    $data .= '  <label for="input-for-age" class="col-sm-2 text-right control-label" style="padding-top:5px;">Age <span class="required">*</span></label>';
    $data .= '  Age: <input type="checkbox" id="myCheck" name="AgeCheck" onclick="showCheckbox()">';
    $data .= '  <p id="text" style="display:none; color:red;">Check Enabled</b><br><input type="number" placeholder="Enter Number"></p><br>';
}
for($x = 1; $x <= $qty; $x++){
    $data .= '<b style="margin-left:10px;">Person ' . $x . '</b>';
    $data .= '<div class="form-group" style="padding-top:10px;">';
    $data .= '  <label for="input-for-age" class="col-sm-2 text-right control-label" style="padding-top:5px;">Age <span class="required">*</span></label>';
    $data .= '  Age: <input type="checkbox" id="myCheck'.$x.'" name="AgeCheck'.$x.'" onclick="showCheckbox('.$x.')">';
    $data .= '  <p id="text'.$x.'" style="display:none; color:red;">Check Enabled</b><br><input type="number" placeholder="Enter Number"></p><br>';
}
function showCheckbox(x) {
    // Get the checkbox
    var checkBox = document.getElementById("myCheck"+x);
    // Get the output text
    var text = document.getElementById("text"+x);

    // If the checkbox is checked, display the output text
    if (checkBox.checked == true){
        text.style.display = "block";
    } else {
        text.style.display = "none";
    }
}