[英]Get the string, but not the value from dropdown list option select
I have two select box, and one hidden text box 我有两个选择框和一个隐藏的文本框
<select name="name" id="name" class="form-control input-sm">
<option value="">- Choose a Name -</option>
<?php foreach($name as $n) { ?>
<option value="<?php echo $n->id_name;?>"><?php echo $n->name;?></option>
<?php } ?>
</select>
<select name="status" id="status" class="form-control input-sm">
<option value="">- Choose a Status -</option>
<?php foreach($status as $s) { ?>
<option value="<?php echo $s->id_status;?>"><?php echo $s->name;?></option>
<?php } ?>
</select>
<input type="hidden" id="message" name="message" value="">
What I know is the script like this 我知道的是这样的脚本
function inputKelurahan()
{ var name = document.getElementById("name");
var status = document.getElementById("status");
document.getElementById("message").defaultValue = name.value + status.value;
}
Of course, the message would be $id_name + $id_status. 当然,该消息将是$ id_name + $ id_status。 How to concate $name + $status?
如何连接$ name + $ status?
Thanks in advance, 提前致谢,
It sounds as you want the text inside the selected option. 听起来就像您希望所选选项中的文本一样。 Then you can first get the
selectedIndex
, and then the innerHTML
for the option. 然后,您可以首先获取
selectedIndex
,然后获取该选项的innerHTML
。
Below is a simple snippet, with parts of your code. 以下是一个简单的代码段,其中包含部分代码。
function inputKelurahan() { var name = document.getElementById("name"); var status = document.getElementById("status"); var nameInnerHtml = name.options[name.selectedIndex].textContent; // using textContent as mentioned by james.brndwgn in comments. var statusInnerHtml = status.options[status.selectedIndex].textContent; alert("Name is: " + nameInnerHtml + ". Status is: " + statusInnerHtml); }
<select name="name" id="name" class="form-control input-sm"> <option value="">- Choose a Name -</option> <option value="Value1">Name 1</option> <option value="Value2">Name 2</option> <option value="Value3">Name 3</option> </select> <select name="status" id="status" class="form-control input-sm"> <option value="">- Choose a Status -</option> <option value="StatusValue1">Status 1</option> <option value="StatusValue2">Status 2</option> <option value="StatusValue3">Status 3</option> </select> <button onclick="inputKelurahan()">Click me!</button>
function inputKelurahan()
{ var name = document.getElementById("name");
var status = document.getElementById("status");
document.getElementById("message").value = name.selectedIndex.value + status.selectedIndex.value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.