下拉列表选择获取文本值而不是指定的值

时间:2014-04-25 08:48:28

标签: javascript php html

我只是想知道这是否可行,假设我有:

<select name = 'region' id = 'region'>
  <option value = '1'>Region 1</option>
<select>

现在,我知道当我选择&#34;区域1&#34;时,我得到值1。有办法得到 &#34;区域1&#34;作为值本身而不改变值=&#39; 1&#39;。我需要javascript用于其他下拉菜单。

抱歉,我忘了提及,我指的是PHP。我知道:

$value = $_POST['region'];

将值设为1,如何才能获得传递$ _POST的文本?

9 个答案:

答案 0 :(得分:1)

var el = document.getElementById('region');
var text = el.selectedIndex == -1 ? null : el.options[el.selectedIndex].text;
console.log(text);

答案 1 :(得分:1)

跨浏览器:

var select = document.getElementById('region');
if(select.addEventListener) {
    select.addEventListener('change', function(evt) {     
        var target = evt.target || evt.srcElement;
        var selected = target[target.selectedIndex];
        var text = selected.textContent || selected.innerText;
        alert(text);
    });
 }
else if(select.attachEvent) {
  select.attachEvent('onchange', function(evt) {
        var target = evt.target || evt.srcElement;
        var selected = target[target.selectedIndex];
        var text = selected.textContent || selected.innerText;
        alert(text);
    });
}

http://jsfiddle.net/LrETq/

答案 2 :(得分:1)

使用Javascript:

var value1 = document.getElementById("region");
var value2 = value1.options[value1.selectedIndex].text;
alert(value2);

答案 3 :(得分:1)

JS

$("#region").change(function(){
  var domNode = document.getElementById("region");
  var value = domNode.selectedIndex;
  var selected_text = domNode.options[value].text;
  alert(selected_text);
});

答案 4 :(得分:1)

首先,由于您尚未提及是否计划支持旧版浏览器,我决定也添加对该浏览器的支持。我的脚本适用于所有IE版本(包括IE7)。

因此,首先我们将eventlistener附加到您的select元素。然后我们检索所选选项的文本并返回值;

看看这个=&gt; DEMO

如果您想通过$_POST提交,请执行以下操作 - &gt;

  1. 创建hidden input元素,将其name属性设置为select,然后将其值设置为text变量的值(我们将使用该变量)当我们选择其中一个选项时) - 请参阅下面的代码。
  2. 提交表单后,您可以检索如此值=&gt; $_POST['select']select是我们分配给name元素的attribute hidden input
  3. <强>的Javascript

    //attaching the eventlistener (modified the code to make it compatible with older IE versions)
    
    function attach(element,listener,ev,tf){
    
        if(element.attachEvent) {
    
            //support for older IE (IE7 inclusive)
            element.attachEvent("on"+listener,ev);
    
            }else{
    
            //modern browsers
            element.addEventListener(listener,ev,tf);
    
            }
    
        }
    
    function returnTextofTheSelectedElement(sel){
    
        //getting and returning the text of the selected option
        selectedIndex = sel[sel.selectedIndex];
        return text = selectedIndex.innerText ? selectedIndex.innerText : selectedIndex.textContent;
    
        }
    
    var select = document.getElementById('region');
    
    attach(select,'change',function(){
    
        //pass the select tag you want to get the text of
        //*returnTextofTheSelectedElement* function returns our text
        alert(returnTextofTheSelectedElement(select));
    
        //so you can store it in a *variable* and use it when submitting your form
        text = returnTextofTheSelectedElement(select);
    
        //if you want to submit it via $_POST than do the following
        //create a hidden *input* element, set its name attribute to say *select*, then set its value to the value of our *text* variable
    
        input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'select';
        input.value = text;
    
        select.parentNode.appendChild(input);
    
        alert('The value/text of the hidden input is '+input.value);
    
        //when submitting the form, it will also submit out hidden input with the value (text) of the selected option
        //you can retrieve the value like so => *$_POST['select']* 
    
    
    },false);
    

    <强> HTML

    <select name='region' id='region'>
    
        <option value='1'>Region 1</option>
        <option value='2'>Region 2</option>
        <option value='3'>Region 3</option>
    
    <select>
    

答案 5 :(得分:0)

$(select#region option).click(function(){

          var textOfTheSelectedOption= $(this).text();
          alert( textOfTheSelectedOption);

});

答案 6 :(得分:0)

您可以获得这样的文字值。有用吗?

var e = document.getElementById("region");
var value = e.options[e.selectedIndex].text;
console.log(value);

答案 7 :(得分:0)

<script>

    function getSelectedText(){
   var theSelect= document.getElementById("region");
var theText= theSelect.options[theSelect.selectedIndex].text;
alert(theText);

}
</script>
<select name = 'region' id = 'region'  onchange="getSelectedText()">
  <option value = '1'>Region 1</option>
  <option value = '2'>Region 2</option>

<select>

答案 8 :(得分:0)

<form method="post" action="action.php">
<select name = 'region' id = 'region'  onchange="fnc()">
  <option value = '1'>Region 1</option>
  <option value = '2'>Region 2</option>
<select>
<input type="hidden" name="hidden_region" id="hidden_region">
<input type="submit" value="Send">
</form>
<script type="text/javascript">
function fnc(){
var el = document.getElementById('region');
var text = el.selectedIndex == -1 ? '' : el.options[el.selectedIndex].text;
document.getElementById('hidden_region').value = text;
}
</script>
php帖子中的

就像

$value = $_POST['hidden_region'];