0

I have multiple forms in a single page.
Every form is is in different <div>, I want to insert some values in html inputs, present in a specific <div>, Please tell what I am doing wrong?

function onLoadPopulateInputs(){
    var digits = getDateTimeOnLoad();
    console.log("DT "+digits)
    $('.input').value = digits #I think here is someting wrong!!!
    }

google.setOnLoadCallback(onLoadPopulateInputs);

My Input looks like this:

<input size="16" type="text" readonly>
4

5 回答 5

2
$('#yourDiv > input').val(digits)

your selector is borked

于 2013-06-26T10:39:53.917 回答
1
function onLoadPopulateInputs(){
    var digits = getDateTimeOnLoad();
    console.log("DT "+digits);
    var $yourDiv = $('#yourDiv');
    $yourDiv.find('input').val(digits);
}

google.setOnLoadCallback(onLoadPopulateInputs);
于 2013-06-26T10:42:30.457 回答
1

I would add a class for div containing 'forms' :

<div class="formDiv">
    <input />
    ....
</div>


<div class="formDiv">
    <input />
    ....
</div>

JS

$('.formDiv').each(function(){
    var $form = $(this);
    $form.find('input').each(function(){
        $(this).val(someValue);
    });
});

Usage exemple http://jsfiddle.net/techunter/EzWpu/

Why this instead of just $('input') or $('#myDiv input') like proposed by other?

Well because here you can manage context and control everything. also you said you have multiple forms inside multiple div : "multiple" == make it generic and use class not id. If you need to edit a specific value maybe consider faster selection using ID or $('#myDiv input[name="myInputName"]) or equivalent

于 2013-06-26T11:01:58.190 回答
0

You need to use .val

$('.input').val(digits);
于 2013-06-26T10:39:40.297 回答
0

give your specific div an id and call an id selector..

function onLoadPopulateInputs(){
  var digits = getDateTimeOnLoad();
  console.log("DT "+digits)
   $('#divID input').val(digits);
}

jquery method of setting a value of input is .val() and not .value

于 2013-06-26T10:40:37.543 回答