如何在php中动态地增加选择框选项和字段

How to multiply select box option and a field dynamically in php

本文关键字:选择 选项 字段 增加 php 动态      更新时间:2023-09-26

我有一个像

这样的选择框

<select name="abc">
  <option> 0 </option>
  <option> 1 </option>
  <option> 2 </option>
  <option> 3 </option>
</select>

和普通文本值,如1000。我想在没有表单和提交按钮的情况下动态地在文本框中选择的选项乘以1000。我怎么能做到呢?

请帮助。

谢谢

使用下面的代码

function fun(val)
{
  document.getElementById("answersval").value = val * 1000;
}
<select name="abc" onChange="fun(this.value)">
  <option value="0"> 0 </option>
  <option value="1"> 1 </option>
  <option value="2"> 2 </option>
  <option value="3"> 3 </option>
</select>
<input type="text" name="answersval" id="answersval">

使form1.html如下

<html><head>
<script type="text/javascript">  
function myFunction(val) {
     var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                alert(this.responseText);
            }
        };
        xmlhttp.open("GET", "ajax_create.php?name="+val, true);
        xmlhttp.send();

        /*xmlhttpCreate.open("GET", "ajax_create.php?name=" + val, true);  // Open the request
        xmlhttpCreate.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlhttpCreate.send();*/
}
</script>
</head>
<body>
<select name="abc" onchange="myFunction(this.value)">
  <option> 0 </option>
  <option> 1 </option>
  <option> 2 </option>
  <option> 3 </option>
</select>
</body>
</html>

现在使ajax_create.php如下所示

<?php
$value=$_GET['name']*1000;
echo $value;    
?>