如何使用HTML中的按钮从select中获取值?

How can I get a value from select using a button in HTML?

本文关键字:获取 select 按钮 何使用 HTML      更新时间:2023-09-26

我想在我的代码中添加一个按钮:

<div class="content" data-role="content" id="content" >
         <div id="car">
            <select name="selectCar" class="span12" id="Options"  >
            <option value="-1">Bir istasyon seçiniz.</option>
            <option value="1">Mimarlık</option>
            <option value="2">Yurtlar</option>
            <option value="3">Bilgisayar Mühendisliği</option>
            <option value="4">Kimya Mühendisliği</option>
            <option value="5">Rektörlük</option>
            </select>
         </div>
         <div id="cinfo"></div>
       <button onclick="javascript:callCarInfo(this);">Call PodCar</button>
</div>

当我单击按钮时,我想将<select>元素的值发送给我的函数。有人能帮我一下吗?

可以使用function.call发送值。我很确定你们这么做的实际目的。你甚至可以在函数内部直接访问select的值。

函数。调用

http://jsfiddle.net/8a3zJ/

<button onclick="javascript:callCarInfo.call(this,document.getElementById('Options').value);">Call PodCar</button>
function callCarInfo(arg)
{
    alert(arg);
}

这里的this函数将是按钮上下文本身和arg将有您的下拉选择值。

当你点击你的按钮时,你可以使用jquery来获得选中的选项。

试试这个函数:

 function callCarInfo()
 {
     alert($('#Options :selected').val());
 }

下面是一个实例。

jsfield

function callCarInfo(obj) {
    var content = document.getElementById('content');
    var span12 = document.getElementsByClassName('span12')[0];
    obj.innerHTML = span12.value;
}