douqian1296 2018-12-05 22:24
浏览 65
已采纳

根据PHP MySQL表中的值选择JavaScript值

I'm trying to have a select box with a value that shows based on if the database has a "yes" or "no" in the column. I cannot figure out what my error is in my syntax, but this will not work. I am using JavaScript values to fill all of my table.

tbl +='<td ><select name="status"><option'+if (val['Col4'] == "yes"): + 'selected>yes</option><option'+if (val['Col4'] == "no"): +'selected>no</option></select></td>';

Here is my table:

tbl +='<tr row_id="'+row_id+'" style="background color:'+val['default_color']+'">';
tbl +='<td ><div col_name="Col1">'+val['Col1']+'</div </td>';
tbl +='<td ><div col_name="Col2">'+val['Col2']+'</div></td>';
tbl +='<td ><div col_name="Col3">'+val['Col3']+'</div></td>';
tbl +='<td ><select name="status"><option'+if (val['Col4'] == "yes"): + 'selected>yes</option><option'+if (val['Col4'] == "no"): +'selected>no</option></select></td>';                     
tbl +='<td >;
tbl +='</tr>';

But my table crashes.
The PHP is working for all the other parts of the table. It's getting through an ajax request and then saving the value.

So val['Col4'] will work as a text input, but not as a select box selection.

  • 写回答

1条回答 默认 最新

  • douzhao1912 2018-12-05 22:48
    关注

    Use a conditional (aka "tertiary") expression. You need to add both options, not a single option with a conditional in it. Then each option conditionally adds selected.

    tbl +='<td ><select name="status">';
    tbl += '<option' + (val['Col4'] == "yes" ? ' selected' : '') + '>yes</option>';
    tbl += '<option' + (val['Col4'] == "no" ? ' selected' : '') + '>no</option>';
    tbl += '</select></td>';
    

    You were also missing a space between option and selected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?