Frage

Ich erstelle dynamisch Formfelder.


Wenn ich benutze

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

Es wird teilweise funktionieren. Jetzt muss ich die ID/den Namen des Formfelds dynamisch erhalten. Wie mache ich das?

War es hilfreich?

Lösung

Versuche dies ?

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

Andere Tipps

Ihre Frage ist bestenfalls vage. Aber ist das etwas in der Reihe dessen, was Sie wollen ?:

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

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



Aktualisieren:

Sie können Elemente zu Werten abgleichen:

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

Wird passen:

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

Aber nicht:

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

Siehe das Auswahldokumentation.

NOTIZ: Diese Selektoren sind langsam und ich würde sie nur als letztes Mittel verwenden. Um die Leistung zu beschleunigen, können Sie Abfragen auf einer Teilmenge von Dom -Knoten durchführen:

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

Dies funktioniert auch:

<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>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top