jQuery 按名称选择 div 中的输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13256372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select input within a div by name
提问by DavidTonarini
I need to update the value of some input fields of a form, using values from a second (dynamically generated) div.
我需要使用来自第二个(动态生成的)div 的值来更新表单的某些输入字段的值。
Here's the code
这是代码
$("#second_update_form_data :input").each(function(index) {
//find all input fields in the second div
var FieldName = $(this).attr("name");
var FieldValue = $(this).val();
//try to get the corresponding field from the original form to change it
var Selector = "#TT_CartForm__form input[name=" + FieldName + "]";
var OriginalFiled = $(Selector);
This does not work, but I'm not sure why. I could add an id to the original input fields and use that to select them, but I'd prefer to understand what's wrong and use the correct selector.
这不起作用,但我不确定为什么。我可以在原始输入字段中添加一个 id 并使用它来选择它们,但我更愿意了解有什么问题并使用正确的选择器。
Thanks in advance, kind regards
提前致谢,亲切的问候
EDIT: I'm sorry, I realize I didn't specified which part doesn't work. It is the last line, when I try to select using Selector, so it's this last one that seems to be wrong
编辑:对不起,我意识到我没有指定哪个部分不起作用。这是最后一行,当我尝试使用 Selector 进行选择时,最后一行似乎是错误的
采纳答案by SpYk3HH
UPdated based on your update
根据您的更新更新
var OriginalFiled = $('#TT_CartForm__form input[name="' + FieldName + '"]');
Try changing your quotations around to ensure string vaiable read. something else to keep in mind, IE7 and IE8 support attribute selectors only if a !DOCTYPE is specified. and in IE6- they dont work ata all
尝试更改您的引号以确保可以读取字符串。还有一点要记住,IE7 和 IE8 仅在指定了 !DOCTYPE 时才支持属性选择器。并且在 IE6 中 - 他们根本不工作
Also double check your element ID name, I notice it has two areas that are underscore and one area has 2 underscores. Make sure the div holding your input has that exact same id value
还要仔细检查您的元素 ID 名称,我注意到它有两个下划线区域,一个区域有 2 个下划线。确保保存输入的 div 具有完全相同的 id 值
回答by micadelli
Slightly faster selector:
稍快的选择器:
$('#second_update_form_data').find('input').each(function(index) {
//find all input fields in the second div
var fieldName = $(this).attr('name'),
fieldValue = $(this).val(),
//try to get the corresponding field from the original form to change it
selector = 'TT_CartForm__form input[name=' + fieldName + ']',
originalFiled = $(selector);
回答by keyboardSmasher
Your problem is the dynamically generated divs. You need to use .on()
您的问题是动态生成的 div。你需要使用 .on()
回答by DickieBoy
change:
改变:
$("#second_update_form_data :input")
to
到
$("#second_update_form_data input")