最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - how to set style to table row with inline javascript if statement - Stack Overflow

programmeradmin1浏览0评论

I want to set background color to ash if one of the variable value is 1 else row background color would be white

But the thing is when the variable value is 1 then row has been added but not the contents. If the variable value is 0 all is working as expected

I have tried the following code:

function insertRow(value){
  $('#invoiceTable').append(
    value === 1 ? '<tr style="background-color:	#ccc8cb">' : '<tr>'+
      '<td> category </td>' +
      '<td> material </td>' +
      '<td> ups </td>' +
    '</tr>'
  );
}
<script src=".1.0/jquery.min.js"></script>
<table id="invoiceTable" class="table table-bordered table-hover" >
  <thead>
      <tr>
          <th>Category</th>
          <th>Material</th>
          <th>ups</th>
      </tr>
  </thead>
  <tbody>
  </tbody>
</table>
 
<input type=button value='insert ash row' onclick='insertRow(1)'/>
 
<input type=button value='insert white row' onclick='insertRow(0)'/>

I want to set background color to ash if one of the variable value is 1 else row background color would be white

But the thing is when the variable value is 1 then row has been added but not the contents. If the variable value is 0 all is working as expected

I have tried the following code:

function insertRow(value){
  $('#invoiceTable').append(
    value === 1 ? '<tr style="background-color:	#ccc8cb">' : '<tr>'+
      '<td> category </td>' +
      '<td> material </td>' +
      '<td> ups </td>' +
    '</tr>'
  );
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="invoiceTable" class="table table-bordered table-hover" >
  <thead>
      <tr>
          <th>Category</th>
          <th>Material</th>
          <th>ups</th>
      </tr>
  </thead>
  <tbody>
  </tbody>
</table>
 
<input type=button value='insert ash row' onclick='insertRow(1)'/>
 
<input type=button value='insert white row' onclick='insertRow(0)'/>

Please point what I'm doing wrong.

Share Improve this question edited Oct 19, 2018 at 14:31 Mamun 69k9 gold badges51 silver badges62 bronze badges asked Oct 19, 2018 at 11:56 noonenoone 6,5682 gold badges47 silver badges54 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Putting () around value === 1 ? '<tr style="background-color:ash">' : '<tr>' solves the issue:

(value === 1 ? '<tr style="background-color:ash">' : '<tr>')

What your code is doing is the following, if the value === 1 then insert '<tr style="background-color:ash">' otherwise insert this:

'<tr>'+
      '<td> category </td>' +
      '<td> material </td>' +
      '<td> ups </td>' +
    '</tr>'

So putting brackets around it will make sure it uses '<tr style="background-color:ash">' or '<tr>' and after that it will insert the table with your data.

You need to use round brackets to specify what part of your conditional (ternary) ? is added as the prefix:

(value === 1 ? '<tr style="background-color:lime">' : '<tr>')+...

At the moment you have it such that if the value is 1 it will append <tr style="background-color:ash> without the concatenated item. Thus, you can use brackets to specify that the concatenated item is added to the result of your ternary operator (?)

See example below: (note I changed background:ash to background:lime as ash isn't a valid css color name)

function insertRow(value) {
  $('#invoiceTable').append(
    (value === 1 ? '<tr style="background-color:lime">' : '<tr>') +
    '<td> category </td>' +
    '<td> material </td>' +
    '<td> ups </td>' +
    '</tr>'
  );
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="invoiceTable" class="table table-bordered table-hover">
  <thead>
    <tr>
      <th>Category</th>
      <th>Material</th>
      <th>ups</th>
    </tr>
  </thead>
  <tbody>
</table>

<input type=button value='insert ash row' onclick='insertRow(1)' />

<input type=button value='insert white row' onclick='insertRow(0)' />

The Conditional (ternary) operator is not returning any value. You can assign the value returned from the operator to a variable so that you can use it later.

Please Note: There is no such background-color value ash. Try some other value.

function insertRow(value){
  var tr = value === 1 ? '<tr style="background-color:lightgray">' : '<tr>';
  $('#invoiceTable').append(
    tr +
      '<td> category </td>' +
      '<td> material </td>' +
      '<td> ups </td>' +
    '</tr>'
  );
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table id="invoiceTable" class="table table-bordered table-hover" >
  <thead>
      <tr>
          <th>Category</th>
          <th>Material</th>
          <th>ups</th>
      </tr>
  </thead>
  <tbody>
  </tbody>
</table>
 
<input type=button value='insert ash row' onclick='insertRow(1)'/>

<input type=button value='insert white row' onclick='insertRow(0)'/>

发布评论

评论列表(0)

  1. 暂无评论