gpt4 book ai didi

javascript - 从下拉列表中选择项目时显示文本框 html php?

转载 作者:行者123 更新时间:2023-12-03 03:17:18 27 4
gpt4 key购买 nike

 <div class="control-group">
<label class="control-label">
<span class="red">*</span>Camera name</label>
<div class="controls">
<select name="cam_name" class="span3" id = "ddlPassport" onchange = "ShowHideDiv()">
<option>Select camera:</option>
<option>canon</option>
<option>nicon</option>
<option>sony</option>
<option>pentex</option>
<option>olympus</option>
<option>others</option>
</select>
</div>
</div>
<div class="control-group" id="dvPassport" style="display: none">
<label class="control-label">Your camera name:</label>
<div class="controls">
<input type="text" placeholder="Enter model" name="cam_name" class="input-xlarge">
</div>
</div>

JavaScript

<script type="text/javascript">
function ShowHideDiv() {
var ddlPassport = document.getElementById("ddlPassport");
var dvPassport = document.getElementById("dvPassport");
dvPassport.style.display = ddlPassport.value == "others" ? "block" : "none";
}

php代码

$camname = $_POST['cam_name'];// user name
if(empty($camname)){

$errMSG = "Please enter camera name";

}

当我从下拉列表中选择“其他”时......它工作正常但问题是当我选择 canon 或 nicon else 时...隐藏的 div 的输入标记给出错误..在使用 if(empty($camname)); 检查时在 php 代码中出现错误

最佳答案

选择标签和输入标签具有不同的名称。添加选项值是一个很好的做法

<select name="cam_name" class="span3" id = "ddlPassport" onchange = "ShowHideDiv()">
<option value="">Select camera:</option> // null
<option value="canon">canon</option>
<option value="nicon">nicon</option>
<option value="sony">sony</option>
<option value="pentex">pentex</option>
<option value="olympus">olympus</option>
<option value="others">others</option>
</select>

// give a new name for this input
<input type="text" placeholder="Enter model" name="other_cam_name" class="input-xlarge">

PHP 代码:

 $camname = $_POST['cam_name'];// cam name
$other_camname = $_POST['other_cam_name'];// other cam name
// if both select and input values
if(empty($camname) || ($camname =='others' && empty($other_camname))){
echo $errMSG = "Please enter camera name";
}else{
echo $cam_name = ($camname =='others') ? $other_camname : $camname;
}

关于javascript - 从下拉列表中选择项目时显示文本框 html php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46714216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com