Saya ingin menonaktifkan tombol kirim jika ID rentang par_sku dan chi_sku berisi kata undefined.

Berikut HTMLnya:

<div class='form'>
  <form>
    <div class="form-row">
        <div class="col-md-3"><span>Keyword</span></div>
        <div class="col-md-3"><span>KJHK87JHK</span></div>
        <div class="col-md-3"><span id="par_sku">undefined</span></div>
        <div class="col-md-3"><span id="chi_sku">undefined</span></div>
    </div>
    <div class="form-row">
        <div class="col-md-3"><span>Keyword</span></div>
        <div class="col-md-3"><span>KJH627HJ</span></div>
        <div class="col-md-3"><span id="par_sku">AAA</span></div>
        <div class="col-md-3"><span id="chi_sku">BBB</span></div>
    </div>
    <div class="form-row">
        <div class="col-md-3"><span>Keyword</span></div>
        <div class="col-md-3"><span>KJKHS2726</span></div>
        <div class="col-md-3"><span id="par_sku">CCC</span></div>
        <div class="col-md-3"><span id="chi_sku">DDD</span></div>
    </div>
    <div class='actions'>
      <input id="emptyBut" type="submit" value="Login" disabled="disabled" />
    </div>
  </form>
</div>
0
Elaine Byene 4 Januari 2021, 09:55

2 jawaban

Jawaban Terbaik

Anda dapat menggunakan pemilih contains di jQuery -

$(document).ready(function() {
  var $form = $('.form');
  var isParSkuUndefined = $form.find('span#par_sku:contains(undefined)').length > 0;
  var isChiSkuUndefined = $form.find('span#chi_sku:contains(undefined)').length > 0;
  if (isParSkuUndefined && isChiSkuUndefined) {
    $("#emptyBut").attr('disabled', 'disabled');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
  <form>
    <div class="form-row">
      <div class="col-md-3"><span>Keyword</span></div>
      <div class="col-md-3"><span>KJHK87JHK</span></div>
      <div class="col-md-3"><span id="par_sku">undefined</span></div>
      <div class="col-md-3"><span id="chi_sku">undefined</span></div>
    </div>
    <div class="form-row">
      <div class="col-md-3"><span>Keyword</span></div>
      <div class="col-md-3"><span>KJH627HJ</span></div>
      <div class="col-md-3"><span id="par_sku">AAA</span></div>
      <div class="col-md-3"><span id="chi_sku">BBB</span></div>
    </div>
    <div class="form-row">
      <div class="col-md-3"><span>Keyword</span></div>
      <div class="col-md-3"><span>KJKHS2726</span></div>
      <div class="col-md-3"><span id="par_sku">CCC</span></div>
      <div class="col-md-3"><span id="chi_sku">DDD</span></div>
    </div>
    <div class='actions'>
      <input id="emptyBut" type="submit" value="Login" />
    </div>
  </form>
</div>

Meskipun, Anda harus membuat kelas alih-alih id jika tidak unik.

1
Nikhil Patil 4 Januari 2021, 07:10

Anda bisa mendapatkan text di dalam rentang Anda dan kemudian membandingkannya dengan somevalue(undefined) jika true menyetel beberapa variabel ke false. Bergantung pada tombol tambah atau hapus ini dinonaktifkan dari tombol kirim.

Kode Demo :

var flag = true;
$(".form-row").each(function() {
  //get text and compare
  if (($(this).find(".par_sku").text().trim() == "undefined") && ($(this).find(".chi_sku").text().trim() == "undefined")) {
    flag = false //set flag
  }

})
console.log(flag)
if (!flag) {
  $("#emptyBut").attr('disabled', true) //add
} else {
  $("#emptyBut").removeAttr('disabled') //remove 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='form'>
  <form>
    <div class="form-row">
      <div class="col-md-3"><span>Keyword</span></div>
      <div class="col-md-3"><span>KJHK87JHK</span></div>
      <div class="col-md-3"><span class="par_sku">undefined</span></div>
      <div class="col-md-3"><span class="chi_sku">undefined</span></div>
    </div>
    <div class="form-row">
      <div class="col-md-3"><span>Keyword</span></div>
      <div class="col-md-3"><span>KJH627HJ</span></div>
      <div class="col-md-3"><span class="par_sku">AAA</span></div>
      <div class="col-md-3"><span class="chi_sku">BBB</span></div>
    </div>
    <div class="form-row">
      <div class="col-md-3"><span>Keyword</span></div>
      <div class="col-md-3"><span>KJKHS2726</span></div>
      <div class="col-md-3"><span class="par_sku">CCC</span></div>
      <div class="col-md-3"><span class="chi_sku">DDD</span></div>
    </div>
    <div class='actions'>
      <input id="emptyBut" type="submit" value="Login"  />
    </div>
  </form>
</div>
1
Swati 4 Januari 2021, 07:15