jQuery-在最近/最近的DIV中选择输入&为每个输入添加值
在我的应用程序中,我使用$.get和yahoo API从用户给定的URL获取open-gragh/og:title和description 它可以作为代码片段吹,或者您可以访问 $'get-social-data-from-url'.keyup函数{ var query='从html中选择*,其中url='+$'输入'.val+'和xpath=*'; var url='1〕https://query.yahooapis.com/v1/public/yql?q='+encodeURIComponentquery; $.geturl,functiondata{ var html=$data.find'html'; $'title'.valhtml.findmeta[property='og:title'].attr'content'| |'no title found'; $'description'.valhtml.findmeta[property='og:description'].attr'content'| |'no description found'; }; }; 输入{ 宽度:90%; 高度:50px; 边缘底部:20px; } 标题 描述jQuery-在最近/最近的DIV中选择输入&为每个输入添加值,jquery,html,forms,parent-child,closest,Jquery,Html,Forms,Parent Child,Closest,在我的应用程序中,我使用$.get和yahoo API从用户给定的URL获取open-gragh/og:title和description 它可以作为代码片段吹,或者您可以访问 $'get-social-data-from-url'.keyup函数{ var query='从html中选择*,其中url='+$'输入'.val+'和xpath=*'; var url='1〕https://query.yahooapis.com/v1/public/yql?q='+encodeURICompon
这就是解决问题的方法。首先将数据添加到具有相同值但不同数据名称的每个输入中 例如:
动态创建的元素通常可以具有相同的类,但不能具有相同的ID。如果你因为使用某个插件而无法控制,那么该插件的设计/编写就严重错误。为了方便地管理动态创建的元素,您可以始终使用索引作为基本id的前缀/后缀来创建不同的id。@KingKing感谢您的回复。是的,没错,我有不同的ID,但我不知道IDplugin适用于每个输入。但我仍然可以给他们一个固定的身份证。总之,我在每个输入上都有固定的和不同的ID。感谢您的帮助。
<input class="post-title-create" data-title-id="attributes_1" type="text" id="attributes_1493294690538_title">
$(function () {
var fileFieldId;
var fileFieldVal;
var fileFieldClass;
var query;
var apiUrl;
$(".get-social-data-from-url").keyup(function () {
fileFieldId = $(this).attr('id');
fileFieldClass = $(this).attr('class');
fileFieldVal = $(this).val();
query = 'select * from html where url="' + $(this).val() + '" and xpath="*"';
apiUrl = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query);
$("h4[data-preview-id='" + fileFieldId + "']" ).text('updated');
$("input.post-link-create[data-link-id='" + fileFieldId + "']" ).val(fileFieldVal);
$.get(apiUrl, function(data) {
var html = $(data).find('html');
$("input.post-title-create[data-title-id='" + fileFieldId + "']" ).val(html.find("meta[property='og:title']").attr('content') || 'no title found');
$("textarea.post-description-create[data-description-id='" + fileFieldId + "']" ).val(html.find("meta[property='og:description']").attr('content') || 'no title found');
$("input.post-remote-image-create[data-remoteimg-id='" + fileFieldId + "']" ).val(html.find("meta[property='og:image']").attr('content') || 'no title found');
});
});
});