如何在php foreach循环中获取所选的选项值

时间:2018-05-28 21:08:50

标签: javascript jquery

我的php foreach循环中有一个select选项,如下所示:

<td>
   <select  class="select-tlp form-control">
     <option style="display:none;" selected>'.$tlp.'</option>
     <option>RED</option>
     <option>WHITE</option>
     <option>GREEN</option>
   <select>
</td>

问题是我只需要所选选项的文本,但我的javascript代码会给我所有选择选项文本。

使用Javascript:

$('select').change(function(){
 var closest = $('select').closest('tr');
 var conceptName = $('.select-tlp',closest).find(":selected").text();
 console.log(conceptName);
});

2 个答案:

答案 0 :(得分:2)

我相信您只想查找特定于实例的选项

尝试

    private String[] getList(Data data) {
    String[] restaurentList = null;

    switch (data.getCity()) {

        case "Βουκάτιο":
            restaurentList = getResources().getStringArray(R.array.chalkounia_restaurent);
            TextView btn = (TextView) findViewById(R.id.address);
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Uri uri = Uri.parse("https://www.google.com/maps/place/%CE%92%CE%BF%CF%85%CE%BA%CE%AC%CF%84%CE%B9%CE%BF+%CF%80%CE%B1%CF%81%CE%B1%CE%B2%CF%8C%CE%BB%CE%B1%CF%82/@38.6138245,21.519924,17z/data=!4m13!1m7!3m6!1s0x135e61d52624e3c3:0x58ffbd58963f6548!2zzpXOnyDOkc6zz4HOuc69zq_Ov8-FIM6Yzq3Pgc68zr_PhSwgzqDOsc-BzrHOss-MzrvOsQ!3b1!8m2!3d38.6138203!4d21.5221127!3m4!1s0x0:0xfa280e1b303b4737!8m2!3d38.6115948!4d21.5256091");

                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);

                }
            });
            ImageView img = (ImageView) findViewById(R.id.image);
            img.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Uri uri = Uri.parse("android.resource://com.example.deepanshu.tourguideapp/drawable_hdpi/voukatio");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    if (intent.resolveActivity(getPackageManager()) != null) {
                        startActivity(intent);
                    }


                }
            });

答案 1 :(得分:1)

<td>
   <select  class="select-tlp form-control">
     <option style="display:none;" selected>pleases select</option>
     <option value="red">RED</option>
     <option value="white">WHITE</option>
     <option value="green">GREEN</option>
   <select>
</td>


<script type="text/javascript">
    $('select').change(function(){
        console.log($(this).val());// $(this) -> referrs the 'select' element 
    });
</script>