简体   繁体   English

在javascript函数中插入选择标记

[英]Insert a select tag in a javascript function

I have a javascript function that displays and hides a div after select. 我有一个javascript函数,可以在选择后显示和隐藏div。 The div contains fields of a form. div包含表单的字段。 The div test2 has the fields of the div test1 plus a select which grabs data from a database. div test2具有div test1的字段以及一个从数据库中获取数据的选择。 All of the fields are required. 所有字段都是必需的。

Issue: 问题:

1) I have difficulty to include in formgroup2 variable I mean (test2) ,the select, due to the php code. 1)由于PHP代码 ,我很难将我的意思是(test2),包括在formgroup2变量中

 function hide () {
 console.log("whatever")
 }

function visibilite1(_this) {
var formgroup1 = '<div id="test1" class="divs"><label>Name </label>  
<input type="text" name= "name" id= "name" required>  </br><label>User 
name </label><input type="text" name= "username" id= "username"   
required>  </br><label >Password <em>*</em></label><input 
type="password"   name= "password" id= "password" required></br></div>'

var formgroup2 = '<div id="test2" class="divs"><label>Name </label> 
<input type="text" name= "name" id= "name" required><label>User name 
</label><input type="text" name= "username" id= "username"  required>
<label>Password <em>*</em></label><input type="password" name= "password"
id= "password" required><input type="text" name= "username"  
id=  "username"  required><label>Adress<em>*</em></label><input 
type="text" name= "adress" id= "adress" required></div>'

var formtestElement = document.getElementById('formtest'); 

if (_this.value == "test1") {
  formtestElement.innerHTML = formgroup1
 } else if (_this.value == "test2") {
  formtestElement.innerHTML = formgroup2
 };
 }
 </script>

<select name="role" id="role"
onchange="visibilite1(this);hide()" >
<option value='test1'>Year1</option>
<option value='test2'>Year2</option>
</select>

 <div id="formtest">
<div id="test1" class="divs">
<label>Name </label>
<input type="text" name="name" id="name" required>  </br>
<label>User name </label>
<input type="text" name="username" id="username" required></br>
<label>Password <em>*</em></label>
<input type="password" name="password" id="password" required></br>
</div>
</div>

Every thing works perfectly but I can't include after the address field this following select tag in formgroup2 variable due to the php code: 一切正常,但由于php代码,我无法在formgroup2变量中的以下select标记的地址字段之后添加:

  <label>Client name</label>
  <?php 
  $sql="select ClientName from claims_follow_up.Client where id 
  !='10001' order by ClientName asc "; 
   $req=mysqli_query($dbc,$sql) or die("Erreur d'execution"); 
   ?> 
   <select name="ClientName" id="ClientName"
  onchange="
   var maVal = document.getElementById('ClientName').value;
    if (maVal == 'Create new client')
    {
  window.open('newClientCreate.php', 'blank', 'scrollbars=yes,    
   resizable=no, top=50, left=155, width=1200, height=700');
  };activer()" required disabled="true" onkeypress="activerSubmit()">
  <option value="">select client </option>
   <?php 
   while($d=mysqli_fetch_array($req)) 
   { 
   echo'<option
    value="'.$d['ClientName'].'">'.$d['ClientName'].'</option>'; 
     } 
   mysqli_free_result($req); 

   ?>
 <option value="Create new client"  style="color:blue;    
 font-weight:bold">create new client</option>

 </select>

Your question is not clear, neither your code. 您的问题不清楚,您的代码也不清楚。 I have trouble to understand what you try to do. 我很难理解您的尝试。

Here are some tips to make your code more readable, and your question more accessible to us: 以下是一些技巧,可以使您的代码更具可读性,并且我们可以更轻松地访问您的问题:

  1. Structure your code: Think before coding 结构化代码:编码前先思考
  2. Indent your code in a decent way 以适当的方式缩进代码
  3. Use functions ! 使用功能! Avoid to have such things in code: 避免在代码中包含以下内容:

    onchange="var maVal = document.getElementById('ClientName').value;if (maVal == 'Create new client'){ window.open('newClientCreate.php', 'blank', 'scrollbars=yes, resizable=no, top=50, left=155, width=1200, height=700'); };activer()" onchange =“ var maVal = document.getElementById('ClientName')。value; if(maVal =='Create new client'){window.open('newClientCreate.php','blank','scrollbars = yes,resizable =不,顶部= 50,左侧= 155,宽度= 1200,高度= 700');}; activer()“

  4. Use proper id and class names. 使用正确的ID和类名。 It's hard to understand the purphose of a div wich his id is "test1" or a span class="divs".. that's nonsense 很难理解其ID为“ test1”或span class =“ divs”的div的目的。

  5. Now, when you post here, you should always specify what you are trying to do, and when you provide source code, say us from wich part of your code it's taken. 现在,当您在此处发布内容时,您应该始终指定要执行的操作,并且在提供源代码时,请从已使用的代码的最中间部分告诉我们。 You provided us 2 sample of code. 您向我们提供了2个代码示例。 I'm still asking myself if they are both from the same "test.php" script or if they're part of "test1.php" and "test2.php". 我仍然在问自己,它们是否都来自相同的“ test.php”脚本,或者它们是否属于“ test1.php”和“ test2.php”。

  6. Choose your words. 选择你的话。 Your question is "Insert a select tag in a javascript function" wich in french means "Insérer un select tag DANS une fonction javascript". 您的问题是“在javascript函数中插入选择标记”,法文意味着“在标签上插入DANS une fonction javascript的选择标记 ”。 It would be more correct like this "Insert a select tag with a js function" or even better: "Using javascript to insert select tag in html div" 像这样的“ 使用 js函数插入选择标记”会更正确,甚至更好:“使用javascript在html div中插入选择标记”

I hope this answer will help you to improve your question :) I will be glad to help you with this problem. 希望这个答案可以帮助您改善问题:)我很高兴为您解决此问题。 Clean up your code, and dont hesitate to post it ! 清理您的代码,不要犹豫了! Good luck voisin 祝你好运voisin

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

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