Hello, there was a problem with JS, in which I am very weak, made such a code, which outputs a unique data-id for entering into the database and adds, deletes an element from the field, transferring it to another.
Actually 2 problems:
- With 1 transfer of elements, everything works as it should, but when you transfer an element back, something strange happens with the entry in the database of a unique data-id
- Is there any way to solve the problem with a crutch
var data_post =
, where useritems.shift is repeated 10 times and botitems.shift 10 times (it seems that a cycle is needed here).
Here is the executable code (but for some reason it does not work here, it may not be configured this way)
var useritems = []; var botitems = []; function addtrade(inv, object) { if (inv === "user") { if (useritems.length < 10) { useritems.push($(object).attr("data-id")); $(object).attr("onClick", "removetrade('user', this);"); $(".tradehave").append($(object)); } } else { if (botitems.length < 10) { botitems.push($(object).attr("data-id")); $(object).attr("onClick", "removetrade('bot', this);"); $(".tradewant").append($(object)); } } } function removetrade(inv, object) { if (inv === "user") { useritems.pop($(object).attr("data-id")); $(object).attr("onClick", "addtrade(\'user\', this);"); $(".inventoryload").append($(object)); } else { botitems.pop($(object).attr("data-id")); $(object).attr("onClick", "addtrade(\'bot\', this);"); $(".botinventoryload").append($(object)); } } function tradesend() { if (useritems.length > 0) { var data_post = { w1: useritems.shift(), w2: useritems.shift(), w3: useritems.shift(), w4: useritems.shift(), w5: useritems.shift(), w6: useritems.shift(), w7: useritems.shift(), w8: useritems.shift(), w9: useritems.shift(), w10: useritems.shift(), h1: botitems.shift(), h2: botitems.shift(), h3: botitems.shift(), h4: botitems.shift(), h5: botitems.shift(), h6: botitems.shift(), h7: botitems.shift(), h8: botitems.shift(), h9: botitems.shift(), h10: botitems.shift() }; console.log(data_post); } }
.inventoryload { background: yellow; border: 1px solid; height: 100px; width: 100px; } .botinventoryload { background: yellow; border: 1px solid; height: 100px; width: 100px; } .tradewant { background: orange; border: 1px solid; height: 100px; width: 100px; } .tradehave { background: orange; border: 1px solid; height: 100px; width: 100px; } .inv { float: left; } .trade { float: left; } .item { background: white; border: 1px solid; height: 20px; width: 20px; margin: 2px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="inv"> <div class="inventoryload"> <p class="item" onClick="addtrade('user', this);" data-id="1111"></p> <p class="item" onClick="addtrade('user', this);" data-id="1251"></p> <p class="item" onClick="addtrade('user', this);" data-id="4444"></p> </div> <div class="botinventoryload"> <p class="item" onClick="addtrade('bot', this);" data-id="3251"></p> <p class="item" onClick="addtrade('bot', this);" data-id="8018"></p> <p class="item" onClick="addtrade('bot', this);" data-id="6543"></p> </div> </div> <div class="trade"> <div class="tradehave"> </div> <div class="tradewant"> </div> </div> <button onClick="tradesend();" id="button">Обменять</button>
Here is the code itself:
addtrade(inv, object)
- transfers to the required fieldremovetrade(inv, object)
- removes from the required field, transfers to the initialtradesend()
- on the button that sends the request to the database
var useritems = []; var botitems = []; function addtrade(inv, object) { if(inv === "user"){ if(useritems.length < 10) { useritems.push($(object).attr("data-id")); $(object).attr("onClick", "removetrade(\'user\', this);"); $(".tradehave").append($(object)); } else { alertify.error("Ошибка!"); } } else { if(botitems.length < 10) { botitems.push($(object).attr("data-id")); $(object).attr("onClick", "removetrade(\'bot\', this);"); $(".tradewant").append($(object)); } else { alertify.error("Ошибка!"); } } } function removetrade(inv, object) { if(inv === "user"){ useritems.pop($(object).attr("data-id")); $(object).attr("onClick", "addtrade(\'user\', this);"); $( ".inventoryload" ).append($(object)); } else { botitems.pop($(object).attr("data-id")); $(object).attr("onClick", "addtrade(\'bot\', this);"); $( ".botinventoryload" ).append($(object)); } } function tradesend() { if(useritems.length > 0){ var data_post = {w1: useritems.shift(),w2: useritems.shift(),w3: useritems.shift(),w4: useritems.shift(),w5: useritems.shift(),w6: useritems.shift(),w7: useritems.shift(),w8: useritems.shift(),w9: useritems.shift(),w10: useritems.shift(), h1: botitems.shift(),h2: botitems.shift(),h3: botitems.shift(),h4: botitems.shift(),h5: botitems.shift(),h6: botitems.shift(),h7: botitems.shift(),h8: botitems.shift(),h9: botitems.shift(),h10: botitems.shift()}; console.log(data_post); $.ajax({ type: "POST", url: "tradesend.php", data: data_post, success: function(){ window.location.replace("/"); }, error: function(){ alertify.error("Ошибка!"); } }); } else { alertify.error("Ошибка!"); } }
pop
method of an array does not accept a parameter, it simply deletes the last element of the array and returns it, that is, these two entries are absolutely equivalent:useritems.pop($(object).attr("data-id"));
anduseritems.pop();
- Grundy