Fill in a form with jQuery

后端 未结 9 1977
眼角桃花
眼角桃花 2020-12-31 03:04

I am trying to use jQuery to fill in a form with some default values.

The form is contained in a div which has an id. In fact every element has an id, I did so just t

相关标签:
9条回答
  • 2020-12-31 03:39

    To set the value of an input field its

    $("#coordX_0").val(123)
    
    0 讨论(0)
  • 2020-12-31 03:39

    Use val method to set the text of any input field.

    $("#coordX_0").val(123);
    
    0 讨论(0)
  • 2020-12-31 03:42

    Try this instead, using the val method:

    $("#coordX_0").val('some string or number');
    
    0 讨论(0)
  • 2020-12-31 03:44

    All the answers are the exact same so I thought I'd post something different:

    var inputs_to_values = {
        'coordX_0' : 'some value',
        'coordY_0' : 'some other value',
        'edit_0'   : 'N',
        'remove_0' : '_',
        'add_0'    : '-',
        'go_0'     : 'stop?'
    };
    $('#form_coord_0').find('input').val(function (index, value) {
        return inputs_to_values[this.id];
    });
    

    You can pass .val() a function, whatever is returned from the function for each element will be the new value:

    A function returning the value to set.

    this is the current element.

    Receives the index position of the element in the set and the old value as arguments.

    Source: http://api.jquery.com/val

    The above code expects that each input will have a property in the inputs_to_values object so you can convert the ID of the input to the new value for that input.

    Here is a demo: http://jsfiddle.net/zYfpE/

    0 讨论(0)
  • 2020-12-31 03:44

    Use this instead:

    $("#coordX_0").val("123");
    
    0 讨论(0)
  • 2020-12-31 03:46

    use the val function

    $("#coordX_0").val("123");
    
    0 讨论(0)
提交回复
热议问题