문제

양식 필드를 동적으로 만들고 있습니다.


내가 사용하는 경우

<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/이름을 동적으로 가져와야합니다. 어떻게해야합니까?

도움이 되었습니까?

해결책

이 시도 ?

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

다른 팁

당신의 질문은 기껏해야 모호합니다. 그러나 이것은 당신이 원하는 것의 선을 따라 다소 있습니까? :

$("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" />

참조 선택기 문서.

알아채다: 이 선택기는 느리고 최후의 수단으로 만 사용합니다. 성능 속도를 높이려면 DOM 노드의 하위 집합에서 쿼리를 수행 할 수 있습니다.

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

이것은 또한 작동합니다 :

<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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top