我是Java语言的新手。我想做的是从“文本”字段和表单组,选项中获取值。
这是我的代码示例
<div class='modal fade' id='small?id=<?php <-- Here is what is used to call the #small
$username = $_SESSION['username'];
$db = mysqli_connect("localhost", "root", "password", "testdb");
$sql = "SELECT * FROM user WHERE username = '$username' ORDER BY id DESC";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo '"'.$row["id"].'"';
}
?>' role='dialog'></div>
var phoneNumber = document.getElementById("phoneNumber");
var email = document.getElementById("email");
var type = document.getElementById("typeNotification");
function addButton(){
var typeNote = alert(email);
console.log(email);
console.log(phoneNumber);
}
我看到很少有视频在该行 document.getElementById(“ typeNotification”)中添加 .value -由于某些原因,我不允许添加它。 知道我该怎么办吗?
答案 0 :(得分:0)
.value
是您要查找的属性
/** @type {HTMLElement} */
var phoneNumber = document.getElementById("phoneNumber");
/** @type {HTMLElement} */
var email = document.getElementById("email");
/** @type {HTMLElement} */
var type = document.getElementById("typeNotification");
function addButton(){
// var typeNote = alert(email);
console.log( type.value )
}
<div class="form-group">
<select id="typeNotification" class="form-control">
<option>Shock Sensor</option>
<option>GPS Jamming</option>
<option>Ingition off</option>
<option>Temperture</option>
</select>
</div>
<button type="button" onclick="javascript: addButton()" id="addNotification" class="btn btn-primary" >Add Notification</button>
编辑:添加了文档块
答案 1 :(得分:0)
JavaScript语句document.getElementById(“ ElementID”)用于从表单元素获取值。 请指定您要做什么。
答案 2 :(得分:0)
最简单的方法
如果您只想获取按钮单击时所有表单元素的值,请使用value
来获取按钮单击时该表单元素的值
或对于选择框,您也可以这样获得值:
var myIndex = document.getElementById("typeNotification").selectedIndex;
var myOptions = document.getElementById("typeNotification").options;
这是您的代码的运行摘要
var phoneNumber = document.getElementById("phoneNumber");
var email = document.getElementById("email");
var type = document.getElementById("typeNotification");
function addButton(){
console.log(phoneNumber.value);
console.log(email.value);
//Use this
console.log(type.value);
//Or this
var myIndex = document.getElementById("typeNotification").selectedIndex;
var myOptions = document.getElementById("typeNotification").options;
console.log(myOptions[myIndex].text);
}
<div class="form-group">
<select id="typeNotification" class="form-control">
<option>Shock Sensor</option>
<option>GPS Jamming</option>
<option>Ingition off</option>
<option>Temperture</option>
</select>
</div>
<input type="text" id="phoneNumber" class="form-control" aria-describedby="emailHelp" placeholder="Enter phone number">
<input type="email" id="email" class="form-control" aria-describedby="emailHelp" placeholder="Enter email">
<button type="button" onclick="javascript: addButton()" id="addNotification" class="btn btn-primary" >Add Notification</button>
还,我做了以下更正
var typeNote = alert(email);
id="exampleInputSMS"
id="exampleInputEmail1"
答案 3 :(得分:0)
使用
var index = document.getElementById("typeNotification").selectedIndex
获取所选条目的索引
并使用
var x = document.getElementById("typeNotification").options
获取所有选项
然后像x
一样经过x[index].text
来说出所选条目的文字。