1

This shouldn't be so hard. Thanks for any help.

I'm trying to get the id (form:eq#) of a parent form from with a button action.

So, a when user clicks a button, I need to be able to add the selector (eq) of the form to the button's action.

This is part of the button function and it works:

$('form:eq('+formVal+') input[name='+fieldName+']').val(currentVal + 1);

IF

formVal = a number.

That number needs to be the selector id of the form. You use, I have multiple forms on my page, all using the same jquery script, and I am trying to make sure that when you click a button within a form, the action only applies to one form, not all forms on the page.

form:eq0 triggers the first form on the page. form:eq1 triggers the second form on the page, etc.

I need to make this dynamic.

I have been trying code along the lines of: formVal=$(this).parent('form').attr('id');

...to get formVal that is the numeric selector number of the (parent) form.

How the heck do I do this? Thanks for any help. The complete script is here:

jQuery(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get the form name
        formVal=$(this).parents("form").find('id');
        // Get its current value
        var currentVal = parseInt($('form:eq('+formVal+') input[name='+fieldName+']').val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $('form:eq('+formVal+') input[name='+fieldName+']').val(currentVal + 1);
        } else {
            // Otherwise put a 0 there
            $('form:eq('+formVal+') input[name='+fieldName+']').form.id.val(0);
        }
    });
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 0) {
            // Decrement one
            $('input[name='+fieldName+']').val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    });
});
4

2 に答える 2

0

次のように単純にしてみませんか。

jQuery(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get the form name
        var form=$(this).parents("form");

        var input = form.find("input[name='"+fieldName+"']'");
        // Get its current value
        var currentVal = parseInt(input.val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
           input.val(currentVal + 1);
        } else {
            // Otherwise put a 0 there
            form.attr("id").val(0);
        }
    });
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 0) {
            // Decrement one
            $('input[name='+fieldName+']').val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    });
});

親フォームにアクセスできるので、ID を取得するためだけにアクセスして、その ID を使用してフォームを再度検索するべきではありません。

于 2013-04-28T04:26:49.193 に答える