2009-06-19 220 views
1

我正在动态创建表单域。jQuery和动态生成的表单


如果我使用

<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> 

这将部分工作。现在,我必须动态获取formField的ID /名称。我该怎么做呢?

+0

你的问题就没有意义了。更新它。 – roosteronacid 2009-06-19 19:34:54

+0

对不起,但我不确定你称之为formField的ID /名称,也不知道你动态地输入了什么。 – glmxndr 2009-06-19 19:37:51

+0

不知何故,它没有发布我的服务器端代码。我有

一些ColdFusion的环...
CFNinja 2009-06-19 19:40:17

回答

3

试试这个吗?

$("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); 
     } 
     }); 
    }); 
}); 
1

你的问题充其量是模糊的。但就是这个有点沿着你想要什么?:线

$("input").blur(function() 
{ 
    var that = $(this); 

    var id = that.attr("id"); 
    var name = that.attr("name"); 
}); 



更新:价值观

可以匹配的元素:

$("input[id^='hello']") 

将匹配:

<input type="text" id="helloworld" /> 
<input type="text" id="helloearth" /> 

但不是:

<input type="text" id="hiworld" /> 

selector documentation

通知:这些选择器很慢,我只能用'em作为最后的手段。为了加速性能,您可以对DOM节点的子集进行查询:

$("complex selector goes here", $("container node in which to query")) 
0

这也适用:

<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>