dsadasd12132 2013-09-16 18:10
浏览 30
已采纳

动态获取元素的属性?

I dont know how to search for this, so apologies if this has been asked before. Basically i am using jQuery/AJAX/PHP to dynamically save to a database, pull info back etc. I have made my form save and pull back the room's id, and then the item's id's when they are saved, however i need a way to dynamically save the room id to it's children items so it all links up in the database. I will try and explain everything i can.

<div id="formHolder" rel="1">
            <form class="rooms">
                <label>Room</label>
                <input type="text" name="roomName[]">
                <input type="hidden" class="roomId" name="roomId[]">
                <input type="hidden" class="inventoryId" name="inventoryId[]" value="<?=$_GET['inventory_id']?>">
                <div class="saveRoom">Save Room</div>
            </form>
            <div class="addItem">
                <form class="items">
                    <label>Item</label>
                    <input type="text" name="itemName[]">
                    <label>Description</label>
                    <textarea name="itemDescription[]"></textarea>
                    <label>Condition</label>
                    <select name="itemCondition[]">
                        <option value="Good">Good</option>
                        <option value="Adequate">Adequate</option>
                        <option value="Poor">Poor</option>
                    </select>
                    <input type="hidden" class="itemId" name="itemId[]" value="">
                    <input type="hidden" name="itemInventoryId[]" value="<?=$_GET['inventory_id']?>">
                    <input type="hidden" name="itemParent[]" value="">
                    <div class="saveIcon" style="display: none; color: green;">SAVED!</div>
                    <div class="save">Save Item</div>
                </form>
            </div>
            <div id="addItem">Add another item</div>
        </div>

This is the basic form i am using. My plan here was to save the room's ID to the rel attribute and then each item that is added would grab that id from there, but i simply cannot work out how to do it.

$('#addItem').on('click', function(){
            $('<form class="items">\
                <label>Item</label>\
                <input type="text" name="itemName[]">\
                <label>Description</label>\
                <textarea name="itemDescription[]"></textarea>\
                <label>Condition</label>\
                <select name="itemCondition[]">\
                    <option value="Good">Good</option>\
                    <option value="Adequate">Adequate</option>\
                    <option value="Poor">Poor</option>\
                </select>\
                <input type="hidden" class="itemId" name="itemId[]" value="">\
                <input type="hidden" name="itemInventoryId[]" value="<?=$_GET["inventory_id"]?>">\
                <input type="hidden" name="itemParent[]" value="'$(this).parent().parent().attr('rel');'">\
                <div class="saveIcon" style="display: none; color: green;">SAVED!</div>\
                <div class="save">Save Item</div>\
            </form>').fadeIn(500).appendTo('.addItem');
        });

You can see i've tried using $(this).parent().parent().attr('rel'); but that doesn't work so i am stumped. I know i've waffled a bit here and if anyone wants to open a chat with me please do, thank you for your time

  • 写回答

2条回答 默认 最新

  • douben8492 2013-09-16 18:13
    关注

    It will work fine, but you're going up too many levels! The div with the rel attr is just the parent, not the parents parent:

    $(this).parent().attr('rel');
    

    Or, since your parent has an ID (which better be unique!):

    $("#formHolder").attr("rel");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵