[英]jQuery and dynamically generated forms
I am creating form fields dynamically. 我正在动态创建表单字段。
If I use 如果我使用
<script>
$().ready(function() {
$("input[name=salesPrice1]").blur(function() {
var nameID = $("input[name=salesPrice1]").val();
alert(nameID);
//var newName = $("input#newName").val();
$.ajax({
type: "POST",
url: "cashPrice.cfm",
data: "salePrice=" + $("input[name=salesPrice1]").val(),
cache: false,
success: function(data) {
$("#cashPrice1").html(data);
}
});
});
});
</script>
It will partially work. 它将部分工作。 Now, I have to get the ID/Name of the formField dynamically.
现在,我必须动态获取formField的ID / Name。 How do I do this?
我该怎么做呢?
Try this ? 尝试这个 ?
$("input[name^=salesPrice]").each(function(){
var input = $(this);
var name = input.attr('name');
var num = /\d+$/.exec(name)[0];
$(this).blur(function() {
var nameID = input.val();
alert(nameID);
//var newName = $("input#newName").val();
$.ajax({
type: "POST",
url: "cashPrice.cfm",
data: "salePrice=" + nameID,
cache: false,
success: function(data) {
$("#cashPrice"+num).html(data);
}
});
});
});
Your question is vague at best. 你的问题充其量是模糊的。 But is this somewhat along the lines of what you want?:
但这有点像你想要的那样吗?:
$("input").blur(function ()
{
var that = $(this);
var id = that.attr("id");
var name = that.attr("name");
});
Update: 更新:
You can match elements on values: 您可以匹配值的元素:
$("input[id^='hello']")
Will match: 将匹配:
<input type="text" id="helloworld" />
<input type="text" id="helloearth" />
But not: 但不是:
<input type="text" id="hiworld" />
See the selector documentation . 请参阅选择器文档 。
NOTICE: these selectors are slow and I would only use 'em as a last resort. 注意:这些选择器很慢,我只会使用'em作为最后的手段。 To speed up performance, you can do queries on a subset of DOM nodes:
为了加快性能,您可以对DOM节点的子集进行查询:
$("complex selector goes here", $("container node in which to query"))
this also works: 这也有效:
<html>
<script type="text/javascript">
$().ready(function() {
alert('loaded');
$('.salesPriceInput').blur(function ()
{
alert($(this).attr("id"));
var myID = $(this).attr("id").substr(10,1);
alert(myID);
$.ajax({
type: "get",
url: "cashPrice.cfm",
data: "salePrice=" + $('#salesPrice'+myID).val(),
cache: false,
success: function(data){
$('#cashPrice'+myID).html(data);
}
})
});
});
</script>
<form>
<cfoutput>
<cfloop from="1" to="3" index="i">
<input type="text" name="salesPrice#i#" id="salesPrice#i#" class="salesPriceInput" value="" />
<div id="cashPrice#i#"></div>
<hr />
</cfloop>
</cfoutput>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.