要将两个选择标记的值放在<输出>中,请 HTML
to put the value of two select tag in a <output>, html
我有两个选择标签,我希望当用户在每个选择标签中选择一个值时,有一个显示两个值的输出:
例如,如果在第一个标签中有人选择cars
,而在 seccon golf
中,我想在输出中包含表达式cars,golf
..我该怎么做?请帮助我
这是一个纯JavaScript的工作演示。
我提出的解决方案是捕获onchange
事件并使用 innerHTML
将其显示在div 标签中。
.HTML:
<select id="select1">
<option> cars </option>
<option> bikes </option>
</select>
<div id="output"></div>
JavaScript:
document.getElementById("select1").onchange=function(){
var e = document.getElementById("select1");
selected1 = e.options[e.selectedIndex].text;
if(selected2!="")
document.getElementById("output").innerHTML = selected1 + ", " + selected2;
};
这是一个粗略的想法,你需要根据自己的需要进行细化
<script type="text/javascript">
$(function(){
var sb1_val;
var sb2_val;
$('#sb1').on('change', function()
{
sb1_val = this.value ;
});
$('#sb2').on('change', function()
{
sb2_val = this.value ;
console.log(sb1_val+','+sb2_val); //this will output the both
alert(sb1_val+','+sb2_val); //this will output the both
});
});
</script>
<select id='sb1'>
<option value='car1'>car1</option>
<option value='car2'>car2</option>
</select>
<select id='sb2'>
<option value="golf1">golf1</option>
<option value='golf2'>golf2</option>
</select>
演示
只需使用 jQuery 中的.change()
即可。
$("select").change(function(){
//output the values
$("#my_output").val($("#s1").val() + "," + $("#s2").val());
});
在这里演示。
相关文章:
- 使用Jade输出HTML(在ng个重复内)
- 输出 html 表单数据
- 如何通过 AngularJS 模板输出 html
- Ajax 输出 html 注释
- 显示查询输出 HTML 中的图像
- HTML 代码不适用于 jquery 调用,而是输出 HTML 代码
- Html.ActionLink onclick JavaScript 函数未被调用,并且函数名称在输出 HTML 中已损
- Javascript -使用字符串输出HTML
- 如何正确使用jQuery从php输出html字符串
- 为什么.html()不输出html标签
- 如何获得一个变量's输出html值
- Twitter-text.js输出html作为纯文本
- 多行文本文件输出html
- 如何使用XSLT匹配元素和输出HTML
- 输出HTML代码
- 输出HTML时出现意外的令牌错误
- 用JavaScript输出HTML实体
- 在rjerb模板中输出HTML
- 从angularjs过滤器输出HTML,无需ng绑定HTML
- 获取实际输出HTML的函数