【问题标题】:Javascript - set value of input field equal to value selected from drop down listJavascript - 将输入字段的值设置为等于从下拉列表中选择的值
【发布时间】:2012-05-28 20:23:06
【问题描述】:

我有一个基于 PHP 的表单。它有一个从 mysql 数据库中获取值的下拉列表。

<select name=cat onchange="AjaxFunction(this.value);" style="width=600"> <br>
<option value='' Select One</option> 
<br>
<? 
  require "config.php";// connection to database  
  $q=mysql_query("select cat_id from categories"); 
  while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
  } 
?> 
</select> 

下拉列表 100% 有效。然后我有另一个文本输入字段,

<input type=text name=copycat>

我希望山寨输入框的值等于下拉列表的选中值,并实时更新。

这可以吗?我应该很容易想象。我在想这样的事情:

<input type=text name=copycat onBlur="document.getElementById('cat').value=this.value;">

任何帮助将不胜感激。

感谢和问候, 瑞恩

更新

获取 javscript sendValue 与山寨值一起使用的代码。

catalin87,请协助让 sendvalue 正常工作,目前,单击选择按钮没有来自浏览器的响应。

再次感谢, 瑞恩

<? 

  $con = mysql_connect('localhost', 'username', 'password'); 
 if (!$con) 
   { 
   die('Could not connect to server: ' . mysql_error()); 
   } 
   $db=mysql_select_db("database", $con); 

    if (!$db) 
   { 
   die('Could not connect to DB: ' . mysql_error()); 
   } 


$sql="select packcode,category,description,grouping,packconfig,sellingunits,eottpoints from skudata order by category, packcode";
$result=mysql_query($sql);

 ?> 

<script type="text/javascript"> 

  function AjaxFunction(cat_id) { 
    var httpxml; 
    try { 
      // Firefox, Opera 8.0+, Safari 
      httpxml = new XMLHttpRequest(); 
    } catch (e) { 
      // Internet Explorer 
      try { 
        httpxml = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
        try { 
          httpxml = new ActiveXObject("Microsoft.XMLHTTP"); 
        } catch (e) { 
          alert("Your browser does not support AJAX!"); 
          return false; 
        } 
      } 
    } 
    function stateck() { 
      if (httpxml.readyState == 4) { 
        var myarray = eval(httpxml.responseText); 
        // Before adding new we must remove previously loaded elements 
        for (j = document.testform.subcat.options.length - 1; j >= 0; j--) { 
          document.testform.subcat.remove(j); 
        } 
        for (i = 0; i < myarray.length; i++) { 
          var optn = document.createElement("OPTION"); 
          optn.text = myarray[i]; 
          optn.value = myarray[i]; 
          document.testform.subcat.options.add(optn); 
        }  
      } 
    } 
    var url="dd.php"; 
    url = url+"?cat_id="+cat_id; 
    url = url+"&sid="+Math.random(); 
    httpxml.onreadystatechange = stateck; 
    httpxml.open("GET",url,true); 
    httpxml.send(null); 
  } 

</script> 




 <script type="text/javascript"> 
function updateinput(){ 
var e = document.getElementById("subcat"); 
var catSelected = e.options[e.selectedIndex].value; 

document.getElementById("copycat").value=catSelected; 
} 
</script> 


<script type="text/javascript"> 
function sendValue(value)
 { 
 value = e.options[e.selectedIndex].value; 
 var parentId = <?php echo json_encode($_GET['id']); ?>; 
 window.opener.updateValue(parentId, value); 
 window.close(); 
 } 
 </script> 


<form name="testform">
Category: &nbsp; <select name=cat id=cat onchange="AjaxFunction(this.value);" style="width=600"> <br>
<option value='' style="width=600">Select One</option> 
<br>
<? 

  require "config.php";// connection to database  
  $q=mysql_query("select * from categories"); 
  while($n=mysql_fetch_array($q)){ 
    echo "<option value=$n[cat_id]>$n[category]</option>"; 
  } 

?> 
</select> 
 <br><br>
 Pack Code:
<select name=subcat onchange="updateinput();" > 
 <br><br>
</select>
<br><br>
<input type=text name=copycat id=copycat > 
<br><br>
<td><input type=button value="Select" onClick="sendValue(document.getElementById(copycat))" /></td>
</form> 

【问题讨论】:

    标签: javascript select drop-down-menu input


    【解决方案1】:

    1`st为选择框和输入设置一个id,并添加一个onChange事件:

    <select name=cat id=cat onchange="updateinput();" style="width=600">
    <input type=text name=copycat id=copycat >
    

    然后把这个函数放在某处:

    <script type="text/javascript">
    function updateinput(){
    var e = document.getElementById("cat");
    var catSelected = e.options[e.selectedIndex].text;
    
    document.getElementById("copycat").value=catSelected;
    }    
    </script>   
    

    这将填充所选项目的标签,如果您想要所选项目的值,请使用此函数:

    <script type="text/javascript">
    function updateinput(){
    var e = document.getElementById("cat");
    var catSelected = e.options[e.selectedIndex].value;
    
    document.getElementById("copycat").value=catSelected;
    }
    </script>   
    

    这是您的完整代码:

         <? 
    
      $con = mysql_connect('localhost', 'username', 'password'); 
     if (!$con) 
       { 
       die('Could not connect to server: ' . mysql_error()); 
       } 
       $db=mysql_select_db("dbname", $con); 
    
        if (!$db) 
       { 
       die('Could not connect to DB: ' . mysql_error()); 
       } 
    
    
    $sql="select packcode,category,description,grouping,packconfig,sellingunits,eottpoints from skudata order by category, packcode";
    $result=mysql_query($sql);
    
     ?> 
    
    <script type="text/javascript"> 
    
      function AjaxFunction(cat_id) { 
        var httpxml; 
        try { 
          // Firefox, Opera 8.0+, Safari 
          httpxml = new XMLHttpRequest(); 
        } catch (e) { 
          // Internet Explorer 
          try { 
            httpxml = new ActiveXObject("Msxml2.XMLHTTP"); 
          } catch (e) { 
            try { 
              httpxml = new ActiveXObject("Microsoft.XMLHTTP"); 
            } catch (e) { 
              alert("Your browser does not support AJAX!"); 
              return false; 
            } 
          } 
        } 
        function stateck() { 
          if (httpxml.readyState == 4) { 
            var myarray = eval(httpxml.responseText); 
            // Before adding new we must remove previously loaded elements 
            for (j = document.testform.subcat.options.length - 1; j >= 0; j--) { 
              document.testform.subcat.remove(j); 
            } 
            for (i = 0; i < myarray.length; i++) { 
              var optn = document.createElement("OPTION"); 
              optn.text = myarray[i]; 
              optn.value = myarray[i]; 
              document.testform.subcat.options.add(optn); 
            }  
          } 
        } 
        var url="dd.php"; 
        url = url+"?cat_id="+cat_id; 
        url = url+"&sid="+Math.random(); 
        httpxml.onreadystatechange = stateck; 
        httpxml.open("GET",url,true); 
        httpxml.send(null); 
      } 
    
    </script> 
    
    
    <script type="text/javascript"> 
    function sendValue(value) 
    { 
        var parentId = <?php echo json_encode($_GET['id']); ?>; 
       window.opener.updateValue(parentId, value);
        window.close(); 
    } 
    </script> 
    
     <script type="text/javascript"> 
    function updateinput(){ 
    var e = document.getElementById("cat"); 
    var catSelected = e.options[e.selectedIndex].value; 
    
    document.getElementById("copycat").value=catSelected; 
    } 
    </script> 
    
    <form name="testform">
    Category: &nbsp; <select name=cat id=cat onchange="updateinput();" style="width=600"> <br>
    <option value='' style="width=600">Select One</option> 
    <br>
    <? 
    
      require "config.php";// connection to database  
      $q=mysql_query("select * from categories"); 
      while($n=mysql_fetch_array($q)){ 
        echo "<option value=$n[cat_id]>$n[category]</option>"; 
      } 
    
    ?> 
    </select> 
     <br><br>
     Pack Code:
    <select name=subcat > 
     <br><br>
    </select>
    <br><br>
    <input type=text name=copycat id=copycat >
    <br><br>
    <td><input type=button value="Select" onClick="sendValue('<?php echo $rows['packcode']; ?>')" /></td>
    </form> 
    

    我改变了:

    输入:onchange="updateinput();"

    <select name=cat id=cat onchange="updateinput();" style="width=600">
    

    <input type=text name=copycat id=copycat >
    

    删除:

     onBlur="document.getElementById('cat').value=this.value;"
    

    【讨论】:

    • 你好 catalin87,谢谢。我已经进行了您建议的更改,但没有成功。我还缺少什么吗?我已经用完整的代码 sn-p 编辑了这个问题。谢谢,R
    • Catalin87,你是个传奇。非常感谢,100% 工作。非常感谢您的时间。
    • 嗨 Catalin87,我需要在调用函数 updateinput() 时发送 copycat 的值。 发送值,如何用值替换mysql值packcode的模仿者。再次感谢。
    • 希望我没事,编辑 sendValue 函数如下:&lt;script type="text/javascript"&gt; function sendValue(value) { value = e.options[e.selectedIndex].value; var parentId = &lt;?php echo json_encode($_GET['id']); ?&gt;; window.opener.updateValue(parentId, value); window.close(); } &lt;/script&gt;
    • 嗨@Catalin87,请查看更新以对我的代码提出问题,因为它是当前的。模仿者正确地获得了价值,但是我需要将其发送给父母。单击选择时,提交功能什么也不做。一如既往地感谢您的帮助。谢谢,
    【解决方案2】:

    您应该将id="cat" 添加到:

    <select name="cat" id="cat" onchange="AjaxFunction(this.value);" style="width=600"> 
    

    和:

    echo "<option value='".$n['cat_id']."'>".$n['category']."</option>"; 
    

    演示:http://jsfiddle.net/V94AJ/

    【讨论】:

    • 嗨 MGraph,这对我没有帮助,我提供了完整的代码 sn-p。谢谢一百万。
    • 你好 MGraph,根本没有,山寨输入框保持空白。
    • @Smudger type Select 2 为例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多