简体   繁体   English

jQuery-从外部文件加载后修改html

[英]jquery - modify html after load from external file

I have some javascript in a .js file that include html code from an external file .htm 我在.js文件中有一些JavaScript,其中包括来自外部文件.htm的html代码

After load the html I want to modify the attribute of input text, but I can't do it: 加载html之后,我想修改输入文本的属性,但是我不能这样做:

myScript.js myScript.js

 $(".plus").click(function(){ function addRow() { //I read the current number of rows.. var rowsNumber = $("#rows").val(); //set the progressive number for the new row rowsNumber++; var row = rowsNumber; var row = 1; //load the html from a file $.get("defaultHtmlForRow.htm", function(data) { $("#rowList").after(data); }); //#descriptionDefault is the id of input type text loaded from defaultHtmlForRow.htm $("#descriptionDefault").attr('id', 'description'+row ); //i want to rename the id like id="description1" //#priceDefault is the id of input type text loaded from defaultHtmlForRow.htm $("#priceDefault").attr('id', 'price'+row ); //i want to reename the id like id="price1" //the default value for #priceDefault (now, if correct, #price1) is 30.00, this alert will be tell me "30.00" but I see "undefined" //below, I want to verify that it's all correct var newPrice = $("#price"+row).val(); alert(newPrice); //tell me "undefined"... like I can't read the attribute and value from defaultHtmlForRow.htm } addRow(); }); 

index.htm index.htm的

 <!-- index.htm --> <!-- the first code... --> <input type="hidden" id="rows" name="rows" value="1" /> <div id="rowList"> </div> <script src="path_to/myScript.js"></script> <!-- the end code... --> <!-- end of index.htm --> 

defaultHtmlForRow.htm defaultHtmlForRow.htm

 <!-- the file defaultHtmlForRow.htm --> <div class="form-group" id="rowDefault"> <div class="col-sm-1 col-lg-1"> <input type="text" class="form-control" id="priceDefault" name="priceDefault" placeholder="Es: 30.00" value="" /> </div> <div class="col-sm-1 col-lg-1"> <button type="button" class="btn btn-primary plus"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> </button> </div> </div> 

Use classes in stuff you add. 在添加的内容中使用类。 I "fake" the get and use a hidden element. 我“伪造”获取并使用隐藏的元素。

Note your "alert" shows nothing since the appended value is not set, I "fake" that by putting 3 in for a value. 注意,由于未设置附加值,因此您的“警告”什么也不显示,我通过在值中输入3来“伪造”该内容。

Odd wrap of function in function so I remove that. 函数中的函数奇数换行,因此将其删除。

 $(".plus").on('click', function() { console.log("adding"); //I read the current number of rows.. // might be better to do var rowsNumber = $('.rowDefault').length; var rowsNumber = $("#rows").val(); //set the progressive number for the new row rowsNumber++; var row = rowsNumber; $("#rows").val(row); //set new value //var row = 1; // we fake this, use hidden HTML instead :) //load the html from a file //$.get("defaultHtmlForRow.htm", function(data) { // $("#rowList").after(data); // }); // fake out using class based markup var clonething = $('.hiddengem').find(".form-group.rowDefault").clone(true); clonething.find('.priceDefault').attr('id', 'priceDefault' + row); clonething.find('.priceDefault').attr('name', 'priceDefault' + row); $("#rowList").append(clonething).show(); console.log($("#rowList").find('.rowDefault').length) // this does not exist so comment it out //#descriptionDefault is the id of input type text loaded from defaultHtmlForRow.htm // $("#descriptionDefault").attr('id', 'description' + row); //i want to rename the id like id="description1" //below, I want to verify that it's all correct var newPrice = $("#priceDefault" + row).val(); alert(newPrice); }); 
 .hiddengem { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="hidden" id="rows" name="rows" value="1" /> <div id="rowList"></div> <button class="plus">add one</button> <div class="hiddengem"> <div class="form-group rowDefault"> <div class="col-sm-1 col-lg-1"> <input type="text" class="form-control priceDefault" name="priceDefault" placeholder="Es: 30.00" value="3" /> </div> <div class="col-sm-1 col-lg-1"> <button type="button" class="btn btn-primary plus"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> </button> </div> </div> </div> 

Better: 更好:

 $(".plus").on('click', function() { var n = 'priceDefault' + $('.rowDefault').length; var clonething = $('.hiddengem').find(".form-group.rowDefault").clone(true); clonething.find('.priceDefault').attr('id', n).attr('name', n); $("#rowList").append(clonething); // verify results console.log($("#rowList").find('.rowDefault').length) }); 
 .hiddengem { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="rowList"></div> <div class="col-sm-1 col-lg-1"> <button type="button" class="btn btn-primary plus"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></button> </div> <div class="hiddengem"> <div class="form-group rowDefault"> <div class="col-sm-1 col-lg-1"> <input type="text" class="form-control priceDefault" name="priceDefault" placeholder="Es: 30.00" value="" /> </div> </div> </div> 

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

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