简体   繁体   English

如何使用根据给定变量选择选择选项的选择框返回字符串

[英]how to return string with select box with select option selected as per a given variable

I using Datatable library to show a table in wordpress custom plugin where I need to return selectbox input.我使用 Datatable 库在 wordpress 自定义插件中显示需要返回选择框输入的表格。 The select box option should display the option as it is saved in database but it is showing the first option always.选择框选项应显示该选项,因为它保存在数据库中,但始终显示第一个选项。

How to code the same thing in js when the select input is returned for a column in datatable.当为数据表中的列返回选择输入时,如何在 js 中编写相同的代码。 I have tried the following way:我尝试了以下方法:

columns: [{
       
{
            'data': null,
            'render': function (data, type, row, meta) {
                 
                
            return '<select class="selectpicker" name="pm" id="pm-' + row.mls + '"><option value="Tafolla">Tafolla</option><option value="Lucy">Lucy</option></select>';

            }
         },
}]

Well first get the JS value that is selected then based off return call the php value in a separate call not inlineHTML .好吧,首先获取选择的 JS 值,然后基于在单独的调用中返回调用 php 值而不是 inlineHTML 。 Try not overcomplicate cross language calls ...尽量不要过于复杂的跨语言调用......

 document.getElementById('my-select').addEventListener('change', function() { console.log('You selected: ', this.value); //Call PHP values based on selection eg if(this.value == 2) { var text = " <?php echo ($row['status'] == 'AR')?'selected='selected':''; ?>"; } });
 <select id="my-select"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select>

The other way of doing it is in plain php with HTML select另一种方法是在纯 php 中使用 HTML 选择

<select name="formGender">
   <option value="">Select...</option>
   <option value="M">Male</option>
   <option value="F">Female</option>
 </select>

//PHP
<?php

 if(isset($_POST['formSubmit']) )
  {
   $varGender = $_POST['formGender'];         
  }

?>

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

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