遍历表值以使用jquery更新数据库

时间:2019-04-04 13:57:42

标签: jquery

我正在使用一个表,该表从php服务器中获取数据:

<table id="projects_table" class="table table-striped table-hover table-bordered" width="100%">
          <thead>
            <tr>
              <th>Samba lead domain</th>
              <th>Customer (samba)</th>
              <th>Customer (Dolphin)</th>
              <th>Project name</th>
              <th>Assigned User</th>
              <th>Samba ID</th>
              <th>Opportunity owner</th>
              <th>Created date</th>
              <th>Close date</th>
              <th>Samba stage</th>
              <th>Win ratio (%)</th>
              <th>Order Intake (€)</th>
            </tr>
          </thead>
          <tbody>
          @foreach($messages_only_errors as $key => $project)
            <tr class="item">
              <td><div class="samba_lead_domain">{!! $project['opportunity_domain'] !!}</div></td>
              <td class="customer_name">{!! $project['account_name'] !!}</td>
              <td>
                <select class="customers select2" style="width: 100%;" data-placeholder="Select a customer name">
                  @foreach($customers_list as $key => $value)
                  <option value="{{ $key }}" @if ($value == $project['account_name']) selected @endif>
                    {{ $value }}
                  </option>
                  @endforeach
                </select>
              </td>
              <td class="opportunity_name"><div contenteditable>{!! $project['opportunity_name'] !!}</div></td>
              <td style="width: 200px;">
                <select class="users select2" style="width: 100%;" data-placeholder="Select a user name">
                  @foreach($users_select as $key => $value)
                  <option value="{{ $key }}">
                    {{ $value }}
                  </option>
                  @endforeach
                </select>
              </td>
              <td class="samba_id">{!! $project['public_opportunity_id'] !!}</td>
              <td class="opportunity_owner">{!! $project['opportunity_owner'] !!}</td>
              <td class="created_date">{!! $project['created_date'] !!}</td>
              <td class="close_date">{!! $project['close_date'] !!}</td>
              <td class="stage">{!! $project['stage'] !!}</td>
              <td class="probability">{!! $project['probability'] !!}</td>
              <td class="amount_tcv">{!! $project['amount_tcv'] !!}</td>
            </tr>
          @endforeach
          </tbody>
        </table>

现在,当用户单击按钮时,我想遍历每一行并将值记录在samba Lead域内。

我正在使用以下脚本,但是出现错误,提示它找不到它:

$("#create_project_button").click(function() {
        $("#projects_table tr.item").each(function() {
          var samba_lead_domain = $(this).find("div.samba_lead_domain").html();
          console.log(samba_lead_comain);
        });

谢谢您能告诉我如何获取每一行中不同元素的值...对于选择框,希望它是相同的...

这是我得到的错误:

Uncaught ReferenceError: samba_lead_comain is not defined
    at HTMLTableRowElement.<anonymous> (sambaupload:51207)
    at Function.each (jquery.min.js:2)
    at n.fn.init.each (jquery.min.js:2)
    at HTMLButtonElement.<anonymous> (sambaupload:51205)
    at HTMLButtonElement.dispatch (jquery.min.js:3)
    at HTMLButtonElement.r.handle (jquery.min.js:3)

1 个答案:

答案 0 :(得分:0)

解决方案在选择器中。 您正在动态创建多个行。 每行的第一列的单元格中都带有div“ samba_lead_domain”。

$("#create_project_button").click(function() {
  var selector = "#projects_table tr.item td div.samba_lead_domain";
  var sambaLeadDomains = $(selector);
  sambaLeadDomains.each(function(){
    var div = $(this);
    console.log(div.text());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="projects_table" class="table table-striped table-hover table-bordered" width="100%">
  <thead>
    <tr>
      <th>Samba lead domain</th>
      <th>Customer (samba)</th>
    </tr>
  </thead>
  <tbody>           
    <tr class="item">
      <td>
        <div class="samba_lead_domain">
          Something here
        </div>
      </td>
      <td> 
        random stuff 
      </td>
    </tr>
    <tr class="item">
      <td>
        <div class="samba_lead_domain">
          Something here 2
        </div>
      </td>
      <td> 
        random stuff 2
      </td>
    </tr>
  </tbody>
</table>  
<button id="create_project_button">Create</button>