簡體   English   中英

根據文本字段中的值更改來有條件地更改按鈕上的圖像?

[英]Conditionally change image on a button based on value changing in a text field?

在HTML文件中:

<table>
    <tr>
         <td>
             <button id="btnStatus" title="Status">
                 <img src='images/statDisable.jpg' id=”btnImg”/>
             </button>
         </td>
         <td>
             <label>Price</label>
             <input type="text" id="tbPrice" style="width: 60px"/>
         </td>
    </tr>
</table>

在JS文件中: / *嘗試確保將按鈕圖像設置為默認值禁用,並在更改價格字段中的值時嘗試更改按鈕圖像。 * /

this.init = function(){
   document.getElementById("btnImg ").src='images/statDisable.jpg';
};

$("#tbPrice").blur(function() {
   var price = $("#tbPrice").val();
   if(isNumber(price) || price > 0){
      document.getElementById("btnImg ").src='images/statEnable.jpg';

     /* also tried but did not work
        var img = $('# btnImg), a=img.parent('a');
        var srcVar = 'images/statEnable.jpg';
        img.attr({src:srcVar});
        a.attr("href", srcVar);
     */
  }
});

問題:當用戶在tbPrice中輸入大於零的數值時,我想將btnStatus上的img更改為“ images / statEnable.jpg”。 我收到了價格更改事件,但無法獲取按鈕更改圖像。 我嘗試的示例僅產生帶有“ X”的按鈕

我將函數包裝在文檔中,將isNumber更改為jQuery isNumeric,並更改了||。 至 &&。 我認為以下是您要尋找的:

this.init = function(){
   document.getElementById("btnImg ").src='images/statDisable.jpg';
};

$(document).ready(function(){
  $("#tbPrice").blur(function() {
    var price = $("#tbPrice").val();
    if($.isNumeric(price) && price > 0){
        document.getElementById("btnImg").src='images/statEnable.jpg';
    }
    else{
        document.getElementById("btnImg").src='images/statDisabled.jpg';
    }
  });
});

如果該值為數字和正數,它將更改圖像。 如果值不好,它也可以將其改回禁用狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM