Javascript onchange选择列表

时间:2013-05-14 12:06:01

标签: javascript jquery

请有人帮忙吗?

我有以下选择列表:

<td>Call Lead Type: </td>
<td>
    <select name="CallLeadType" id="CallLeadType">
        <option value=""></option>
        <option value="LSG Service warm transfer from Claims">LSG Service warm transfer from Claims</option>
        <option value="IVR Direct Call">IVR Direct Call</option>
        <option value="Transfer from EE Agent">Transfer from EE Agent</option>
        <option value="Dropped Line">Dropped Line</option>
        <option value="Test Call from EE">Test Call from EE</option>
    </select>
</td>

当用户选择“从EE代理转移”时,我需要显示四个新输​​入框,但在选择任何其他选项时不会显示?

所以我认为它是一个需要的onchange代码?

我只是不确定

提前感谢您的帮助

轧车

7 个答案:

答案 0 :(得分:1)

$('#CallLeadType').on('change', function()
{
  if ( $('.CallLeadType option:selected').val() == 'Transfer from EE Agent' )
  {
    //create inputs here
  }
});

答案 1 :(得分:1)

你可以使用jquery

$('select[name=CallLeadType]').on('change',function(){

 if ( $(this).val() == 'Transfer from EE Agent' )
{
//create inputs here
}

});

答案 2 :(得分:1)

当在组合框中选择特定值时,您将需要一些JavaScript代码来显示其他字段。我使用了简单的javacript代码,以便每个人都能快速理解它。

首先在表格中添加其他文件并使用样式属性display:none

隐藏它们
 <tr id="other_fields" style="display:none">
        <td>Other Fields</td>
        <td>
              <input type="text" value="field1"/>
              <input type="text" value="field2"/>
              <input type="text" value="field3"/>
              <input type="text" value="field4"/>     
        </td>
  </tr>

我已经为此行分配了一个id other_fields,所以在onchange事件中,如果值与所需的值匹配,那么我们将显示它,否则将其隐藏。

其次,添加以下javascript函数并在加载函数中调用它。这会将一个ebent处理程序附加到此选择框。

<script type="text/javascript">
function onload() {
   document.getElementById("CallLeadType").onchange = function (e) {
       if (this.value == 'Transfer from EE Agent') {
          document.getElementById("other_fields").style.display="";
       } else {
          document.getElementById("other_fields").style.display="none";    
      }
  }; 
}
</script>

最后在body标签中调用此函数:

<body onload="onload()">

请参阅以下工作示例:

  

http://jsfiddle.net/YpVGE/1/

希望这有帮助

答案 3 :(得分:0)

$('#CallLeadType').live("change",function(){
   var Id=document.getElementById("CallLeadType");
   var value = Id.options[Id.selectedIndex].text;

   if(value=="Transfer from EE Agent"){

  // your code to add textboxes
}
});

答案 4 :(得分:0)

您可以使用此功能,即使您单击下拉列表并且在模糊事件被触发之前(与简单的onchange事件相比),也应该触发此事件:

{oninput event support:http://help.dottoro.com/ljhxklln.php}

var timeoutChange;
$('#CallLeadType').on('change input paste propertychange', function()
{
  clearTimeout(timeoutChange);
  timeoutChange = setTimeout(function(){
     //code your logic here
  },0);
});

答案 5 :(得分:0)

<td>Call Lead Type: </td>
<td>
    <select name="CallLeadType" id="CallLeadType">
        <option value=""></option>
        <option value="LSG Service warm transfer from Claims">LSG Service warm transfer from Claims</option>
        <option value="IVR Direct Call">IVR Direct Call</option>
        <option value="Transfer from EE Agent">Transfer from EE Agent</option>
        <option value="Dropped Line">Dropped Line</option>
        <option value="Test Call from EE">Test Call from EE</option>
    </select>
    <input type="text" class="transferInput"/>
    <input type="text" class="transferInput"/>
</td>

$(function(){
    $('.transferInput').hide();

    $('#CallLeadType').on('change', function(){
        $('.transferInput').hide();
        if ( $('#CallLeadType').val() == 'Transfer from EE Agent' ){
                 $('.transferInput').show();
          }
    });
});

答案 6 :(得分:0)

你基本上需要两件事,一个事件(。change),你可以在改变元素'select'和伪类的值时触发它( :选中)允许获取选择的当前值,这样您可以创建正确的条件,当您选择“从EE代理转移”时显示四个输入,并在您不选择该选项时隐藏。

以这种方式更改您的Html:

    <td>Call Lead Type: </td>
    <td>
        <select name="CallLeadType" id="CallLeadType">
            <option value=""></option>
            <option value="LSG Service warm transfer from Claims">LSG Service warm transfer from Claims</option>
            <option value="IVR Direct Call">IVR Direct Call</option>
            <option value="Transfer from EE Agent">Transfer from EE Agent</option>
            <option value="Dropped Line">Dropped Line</option>
            <option value="Test Call from EE">Test Call from EE</option>
        </select>

<!-- add the inputs and hide them -->
        <div id="inputsTEEA" style="disply:hide">
            <input type="text"/>
            <input type="text"/>
            <input type="text"/>
            <input type="text"/>
        </div>
    </td>

JavaScript的:

$(function(){
    var $inputsTEEA = $('#inputsTEEA');

    // every time that you choose a different item in the select, this event is triggered
    $('#CallLeadType').change(function(){
        var 
            $select = $(this),
            //pseudo class selected, you get the value of the currently option selected
            valSelect = $select.find('option:selected').val(); 

        if(valSelect == 'Transfer from EE Agent'){
            $inputsTEEA.show();
        }
        else{
            $inputsTEEA.hide();
        }
    });
});
相关问题