简体   繁体   English

jQuery .replaceWith()方法不起作用

[英]jQuery .replaceWith() method not working

I have a problem using the .replaceWith() , it's working for the first blur, but not for the second one, here's my code : 我在使用.replaceWith()遇到问题,它适用于第一次模糊,但不适用于第二次模糊,这是我的代码:

$("#wilaya").blur(function () {
  $("#wilayaRow .tdrequired").css("color", "#333");
  if ($("#wilaya").val() !== "Selectionnez une wilaya...") {
    $("#wilayaRow .tdrequired label").replaceWith("<img src='../../Images/check-mark-md.png' width='20px' height='28px'></img>");
  }
  else {
    $("#wilayaRow .tdrequired label").replaceWith("<img src='../../Images/red-wrong-cross-md.png' width='20px' height='28px'></img>");
  }
});

And here's the html part : 这是html部分:

<table>
  <tr id="wilayaRow">
    <td class="tdtitle">
      <label for="wilaya">Wilaya d'immatriculation</label>
    </td>
    <td class="tdinput">
      <select name="wilaya" id="wilaya">
        <option>Selectionnez une wilaya...</option>
        <option>Khalil</option>
        <option>Moh</option>
      </select>
    </td>
    <td class="tdrequired">
      <label>*</label>
    </td>
  </tr>
  ...
</table>

Thanks for your help in advance :) 谢谢您的帮助:)

Here's the working jQuery code I just implemented : 这是我刚刚实现的有效jQuery代码:

    $("#wilaya").blur(function () {
            $("#wilayaRow .tdrequired").css("color", "#333");
            if ($("#wilaya").val() !== "Selectionnez une wilaya...") {
                    $("#wilayaRow .tdrequired label").replaceWith("<label><img src='../../Images/check-mark-md.png' width='20px' height='28px'></img></label>");
            }
            else {
                    $("#wilayaRow .tdrequired label").replaceWith("<label><img src='../../Images/red-wrong-cross-md.png' width='20px' height='28px'></img></label>");
            }
    });

If I have to replace the label with an image, I must put it in a label, so the next replacement could be possible. 如果必须用图像替换标签,则必须将其放在标签中,以便下次替换。 Otherwise, the label tag won't be found (because it doesn't exist anymore), and it won't be replaced by an image, or anything else. 否则,将找不到label标签(因为它不再存在),并且也不会被图像或其他任何东西替代。

Also the use of : 还可以使用:

    $("#wilaya").prop("selectedIndex") === 0

As a condition is better. 作为条件较好。

$("#wilaya").blur(function () {
  $("#wilayaRow .tdrequired").css("color", "#333");
  if ($("#wilaya").val() !== 0) {
    $("#wilayaRow .tdrequired label").replaceWith("<img src='../../Images/check-mark-md.png' width='20px' height='28px'></img>");
  }
  else {
    $("#wilayaRow .tdrequired label").replaceWith("<img src='../../Images/red-wrong-cross-md.png' width='20px' height='28px'></img>");
  }
});

this is jquery part. 这是jQuery的一部分。

 <table>
  <tr id="wilayaRow">
    <td class="tdtitle">
      <label for="wilaya">Wilaya d'immatriculation</label>
    </td>
    <td class="tdinput">
      <select name="wilaya" id="wilaya">
        <option value="0">Selectionnez une wilaya...</option>
        <option value="1">Khalil</option>
        <option value="2">Moh</option>
      </select>
    </td>
    <td class="tdrequired">
      <label>*</label>
    </td>
  </tr>
</table>

this is html part. 这是html部分。 try out. 试用。 i hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM