简体   繁体   English

如果我有一个项目,如何从选项菜单中选择项目

[英]how can i select item from option menu if i have one item

I want to select an item from option select menu when I have just one item in order to use in my next function ( $('#').on("change", function() {}) ) 当我只有一个项目时,我想从选项选择菜单中选择一个项目,以便在我的下一个函数中使用($('#')。on(“change”,function(){}))

it's work when I have more than one item in the menu but if there is one item when I clicking on that one item there is no any result: 如果我在菜单中有多个项目,但是当我点击该项目时有一个项目没有任何结果,它是有用的:

from the backend item one=[1] two=[1,2] and three=[1,2,3], it is ok for selecting two and three but not for item one which I have only one number inside 从后端项目1 = [1] 2 = [1,2]和3 = [1,2,3],可以选择2和3,但不适用于我只有一个数字的第一项

HTML HTML

<select id = "fir">
<option>one</option>
<option>two</option>
<option>three</option>
</select>

<select id="sec" >
<option ></option>
</select>

<input id="tbl_S2"  type="text" >

JavaScript JavaScript的

$('#fir').on("click", function() {
  var schem =$("#fir").val();

    $.ajax({
    type: 'GET',
    url:'url/'+schem,
    success:function(data2){
        $("#sec").empty();
        for (var i = 0; i < data2.length; i++) {
        $("#sec").append("<option>"+data2[i]+"</option>");
       }
      }
    }                  
   },
  });
});


$('#fir').on("change", function() {
     $('#sec').on("change", function() {

           $('#tbl_S2').html( "hello");

     });
});

In order for a particular item to be selected, you should use the option selected method, ie append selected after data2[i] if there is only one item in the list. 为了要选择一个特定的项目,你应该使用option selected方法,即追加selected后数据2 [I] 如果只有一个列表中的项目。 (add an if statement) (添加if语句)

Your if statement would look similar to this: 你的if语句看起来与此类似:

if (data2.length == 1){
     $("#sec").append("<option selected>"+data2[i]+"</option>");
  }
else{
  for (var i = 0; i < data2.length; i++) {
     $("#sec").append("<option>"+data2[i]+"</option>");
  }
}

The HTML output would look something like this HTML输出看起来像这样

<select>
<option value="data" selected>Your variable</option>
</select>

Hope this helps 希望这可以帮助

EDIT: 编辑:

The below snippet is a simple example of how to show both values from two dropdowns, even when one only has one item in the dropdown. 下面的代码片段是一个简单的示例,说明如何显示两个下拉列表中的两个值,即使下拉列表中只有一个项目。 Please note that in the example, if the second dropdown is changed (if you un-comment back in the second option), the output text doesn't change until the other dropdown is clicked. 请注意,在示例中,如果更改了第二个下拉列表(如果您在第二个选项中取消注释),则在单击其他下拉列表之前输出文本不会更改。 I'm sure I could have corrected this with a little more time spent. 我相信我可以用更多的时间来纠正这个问题。 There is a jsbin here 这里有一个jsbin

I am aware that your list is not static, but dynamic, that you are using ajax to populate, however I hope this provides a starting point. 我知道你的列表不是静态的,而是动态的,你使用ajax来填充,但是我希望这提供了一个起点。

 var secSelect= ""; function showSelect(str) { var schem1=""; var returnText=""; var schem1= str; var sec1=document.getElementById("sec"); var hello=document.getElementById("text1"); var sec2=sec1.value; if (secSelect !=""){ sec1=secSelect; } hello.innerHTML = " hello"; returnText= schem1 +" " +sec2 + hello.value; document.getElementById("text1").innerHTML = returnText; }; function passSelect(str2){ secSelect=str2; } 
 #text1{background-color:white!important; width:150px; display:inline-block; height:13.5px; vertical-align:top;} #sec, #fir{display:inline-block; vertical-align:top; } 
 <select id="fir" onclick="showSelect(this.value);"> <option>one</option> <option>two</option> <option>three</option> </select> <select id="sec" onclick="passSelect(this.value);"> <option>one</option> <!--<option>rach</option>--> </select> <textarea id="text1" type="text"></textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM