[英]Change image button in javascript
我有一個HTML選項表,用戶可以在其中單擊添加按鈕來添加一行:
$('.NewImg').click(function() { var row = $(this).closest('tr').clone(); row.find('input').val(''); $(this).closest('tr').after(row); $('input[type="image"]', row).removeClass('AddNew').addClass('RemoveRow'); }); $('table').on('click', '.RemoveRow', function() { $(this).closest('tr').remove(); }); // everytime the save button is clicked $('#savebutton').click(function() { saveList(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="multiselect" id="order-list"> <tr> <td> <label>Studio:</label> </td> </tr> <tr> <td> <select class="studio"> <option>-- Select Studio --</option> <?php $resultStudio=g etStudioID(); while ($row=m ysqli_fetch_array($resultStudio)) { echo "<option value = '".$row[ 'studio_name']. "'>".$row[ 'studio_name']. "</option>"; } ?> </select> </td> <td> <input id="addrow" class='NewImg' type="image" src="img/add.png" alt="Submit" onClick="return false;" /> </td> <td> <a class="deleteRow"></a> </td> <!--<td><input type='button' class='AddNew' value='Add new item' /></td>--> </tr> </table>
如何更改js代碼中的圖片? 添加一行后,我仍然有一個“ +”圖像,但是,我想將其更改為“-”圖像
使用toggleClass
$('input[type="image"]', row).toggleClass('AddNew RemoveRow');
為每個班級添加不同的背景
沒有名為scr的預定義屬性可用於輸入。 您可以使用CSS在按鈕上添加圖像。
嘗試這個
<button type="button" style="background: url('img/add.png') no-repeat;">+</button>
使用您自己的代碼,我將進行大量重構:
請注意: .attr('src', 'img/remove.png')
顯然,您需要在img
目錄中使用remove.png
圖片
$('.NewImg').click(function() { var row = $(this).closest('tr').clone(); row.find('input').val(''); $(this).closest('tr').after(row); $('input[type="image"]', row).removeClass('AddNew').addClass('RemoveRow').attr('src', 'img/remove.png'); }); $('table').on('click', '.RemoveRow', function() { $(this).closest('tr').remove(); }); // everytime the save button is clicked $('#savebutton').click(function() { saveList(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="multiselect" id="order-list"> <tr> <td> <label>Studio:</label> </td> </tr> <tr> <td> <select class="studio"> <option>-- Select Studio --</option> <?php $resultStudio=g etStudioID(); while ($row=m ysqli_fetch_array($resultStudio)) { echo "<option value = '".$row[ 'studio_name']. "'>".$row[ 'studio_name']. "</option>"; } ?> </select> </td> <td> <input id="addrow" class='NewImg' type="image" src="img/add.png" alt="Submit" onClick="return false;" /> </td> <td> <a class="deleteRow"></a> </td> <!--<td><input type='button' class='AddNew' value='Add new item' /></td>--> </tr> </table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.