我的表单中有此选择
<select id="select" name="gametype" onchange="myFunction(event)">
<option disabled selected value>Please Select Game Type</option>
<option value="28,000" >HD 6 Cameras saterday</option>
<option value="30,000" >HD 6 Cameras</option>
<option value="50,0000">HD 7 Cameras saterday</option>
<option value="32,0000">HD 7 Cameras</option>
<option value="45,000" >4k 7 Cameras saterday</option>
<option value="32,000" >4k 7 Cameras</option>
</select>
我正在$_POST
中传递选项的值。我还如何传递选项文本?例如,如果值是“ 28,800”,我还希望传递“ HD 6摄像机星期六”。
答案 0 :(得分:0)
不确定您如何处理此表单的其他组件,但是我从select元素中删除了onchange属性绑定,只是使用jquery将显示给用户的文本设置为隐藏变量。
<?php
if(isset($_POST['gametype'])) {
var_dump($_POST);
echo "<hr/>";
}
?>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"></script>
<form class="myForm" method="post">
<!--this is empty and stays that way until you submit it-->
<input type="hidden" name="gametype_desc" class="gametype_desc" value="" />
<select id="select" class="selGameType" name="gametype">
<option disabled selected value>Please Select Game Type</option>
<option value="28,000" >HD 6 Cameras saterday</option>
<option value="30,000" >HD 6 Cameras</option>
<option value="50,0000">HD 7 Cameras saterday</option>
<option value="32,0000">HD 7 Cameras</option>
<option value="45,000" >4k 7 Cameras saterday</option>
<option value="32,000" >4k 7 Cameras</option>
</select>
</form>
<script>
$(function(){
$("select.selGameType").change(function(){
//uncomment to log the value in the console
//console.log($(this).children(":selected").text());
//uncomment to log the value in the console
//console.log($(this).val());
//get the text of the currently selected option
selText = $(this).children(":selected").text();
//we set the value of the hidden variable, so that the text (not value) is passed as a separate post var
$("input.gametype_desc").val(selText);
//now you might submit it, or whatever your next step is...
$("form.myForm").submit();
})
})
</script>