简体   繁体   English

页面已加载后,如何使用javascript创建元素后如何选择元素?

[英]How to select an element after creating it with javascript when the page is already loaded?

I made a "Add new shipment" button that creates a whole div that includes like 4 fields and a 2nd submit button under all that. 我做了一个“添加新货件”按钮,该按钮创建了一个整个div,其中包括4个字段,并在其下包括一个第二个提交按钮。 that 2nd submit button is supposed to post this new div onto the page and add it to db but I am having trouble selecting the fields and selecting the new submit button. 该第二个提交按钮应该将这个新的div发布到页面上并将其添加到数据库,但是我在选择字段和选择新的提交按钮时遇到了麻烦。 I think this is because the page is already loaded from the beginning, so this new div cannot be selected anymore? 我认为这是因为页面已经从一开始就被加载了,所以不能再选择这个新的div了吗? Here is my HTML that is created from the "Add new shipment" button: 这是从“添加新货件”按钮创建的HTML:

// adds new shipment
const ADD_SHIPMENT_GRAY = function (){
    return 
    <div class="shipment panel panel-success col-xs-2">
        <div class="shipmentblocks row">
            <div class="idzone btn-block classificationline">
                <!--  label  ---->
                <input type="text" class="customer btn btn-default btn-xs col-xs-12" placeholder="Customer"><strong id="customertext"></strong></button>
                <!--  contents -->
                <input type="text" class="btn btn-default btn-xs col-xs-12" placeholder="File Number">
                    <strong class="boldedlabels" id="filenumber"></strong>
                    <span class="labelcontents"></span>
                </button>
                <button type="button" class="etd btn btn-default btn-xs col-xs-12" placeholder="ETD" id="etd">
                    <strong class="boldedlabels"></strong>
                    <span class="labelcontents" id="etddatepicker">ETD</span>
                </button>
                <button type="button" class="eta btn btn-default btn-xs col-xs-12" placeholder="ETA" id="eta">
                    <strong class="boldedlabels"></strong>
                    <span class="labelcontents" id="etadatepicker">ETA</span>
                </button>
                <button type="button" class="shipmentsubmission btn btn-default btn-xs col-xs-12">
                    <strong class="boldedlabels eta"></strong>
                    <span class="labelcontents" id="submitnewshipment">Submit</span>
                </button>
            </div>
        </div>    
    </div>;
}

Here is my javascript code that executes the above code: 这是我执行上面代码的javascript代码:

const SCRIPTS = function (){
$(function(){
    const addshipment = $("#addshipment"),
          labelsubmission = $(".labelsubmission"),
          shipment = $(".shipment"),
          shipmentblocks = $(".shipmentblocks"),
          idzone = $(".idzone"),
          etd = $(".etd"),
          addsubmit = $("#addsubmit"),
          zzz = $("#zzz"), // div to keep maximum new shipment as 1
          submitnewshipment = $("#submitnewshipment"), // button to submit the new shipment 


    // Add New Shipment
    addshipment.click(() => { // in console, indexes closer to 0 (ex: index [0]) = newer
        //$(".shipment:first").before(ADD_SHIPMENT_GRAY());
        // add new shipment green block
        $("#zzz").html(ADD_SHIPMENT_GRAY());
    });
})
}

You are creating dynamic data which requires you to use .on in jquery, otherwise it will not find the element. 您正在创建动态数据,要求您在jquery中使用.on ,否则将找不到该元素。

Also dont repeat id 's make it a class instead 也不要重复id使其成为类

$(document).on('click', '.addshipment', function () { 
    // in console, indexes closer to 0 (ex: index [0]) = newer
    //$(".shipment:first").before(ADD_SHIPMENT_GRAY());
    // add new shipment green block
    $("#zzz").html(ADD_SHIPMENT_GRAY());
});

http://api.jquery.com/on/ http://api.jquery.com/on/

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

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