Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在向表中添加行之前添加条件:jquery_Javascript_Jquery - Fatal编程技术网

Javascript 如何在向表中添加行之前添加条件:jquery

Javascript 如何在向表中添加行之前添加条件:jquery,javascript,jquery,Javascript,Jquery,我有一个html模板,它由一个表和一个add按钮组成。单击“添加”按钮时,选择将添加到表中。我想添加一个条件,如果其中一个选项包含“Map”,它应该向该特定列添加modal。例如: <div class="reports"> <div class="panel"> <table class="A"> <thead> <td>A</td>

我有一个html模板,它由一个表和一个add按钮组成。单击“添加”按钮时,选择将添加到表中。我想添加一个条件,如果其中一个选项包含“Map”,它应该向该特定列添加modal。例如:

<div class="reports">
    <div class="panel">
        <table class="A">
            <thead>
                <td>A</td>
                <td>B</td>
                <td>C</td>
            </thead>
            <tbody>
            </tbody>
        </table>
   </div>
   <button class="add" type="button">Add</button>
</div>
三列有三个值:
value1、value2、value3

if text2=="Map"{ 
    value2 = "modal-dialog"; 
} else { 
    value2 = text2; 
}

如何在
append()
中添加
if
条件?

而不是在append()中添加条件。尝试在外部调用append()中的元素。 例如:

$('.reports')。在('单击','.add',函数()上{
if(text2==map\u text){
var map_元素+=‘map’;
}否则{
var map_elements+=text2;
}
$(本)
.find('表')
.append(“”+map_elements+“”);//这应该可以
});
使用


我不明白。。。不能在append函数之前使用
if(…){}else{}
语句吗?你有什么不寻常的事情要做吗?纠正@Timleis。在构造表行时,如果。。。else逻辑,以便在追加时表行已准备就绪。或者,如果您处理的是简单字符串,请使用三元运算符
.append(text2=='Map'?'modal dialog':'some other string')
if text2=="Map"{ 
    value2 = "modal-dialog"; 
} else { 
    value2 = text2; 
}
$('.reports').on('click','.add',function(){
  if (text2 == map_text) {
            var map_elements += '<button role="button" data-target="#map-id" class="map-btn btn btn-primary" data-toggle="modal">Map</button></span>';
        } else {
            var map_elements += text2;
        }
 $(this)
         .find('table')
          .append('<tr><td>'+map_elements+'</td></tr>'); //this should work
});
    $('.reports').on('click','.add',function(){
      if text2=="Map"{ 
    do something
} else { 
   do something else
}


    });