2009-06-19 223 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>