How to change text input value using js / jquery?

I would like to dynamically modify the values ​​of certain form elements, or rather, certain text input fields. So far, when I load my html page, I just get an empty input field, but I expect it to contain a value 1. Here is an example of how I am trying to do this.

<!DOCTYPE HTML>
<HTML>
   <HEAD>
      <script type="text/javascript" src="javascript/jquery-1.6.1.min.js"></script>
      <script type="text/javascript">
         $(document).ready(function(){
            var myForm = $(this).getElementById('form1');
            myForm.elements['Q01'].value = '1';
         });
      </script>
   </HEAD>
   <BODY>
      <form id="form1">
         <input type="text" name="Q01" maxlength="1" />
      </form>
   </BODY>
</HTML>

The reason this needs to be done dynamically is because the shape value can be different every time. Am I doing it right? Suggestions on how I can achieve my intended functionality?

- EDIT -

None of the solutions seem to do this. Here is an update to my code:

<!DOCTYPE HTML>
<HTML>
   <HEAD>
      <script type="text/javascript" src="javascript/jquery-1.6.1.min.js"></script>
      <script type="text/javascript">
         $(document).ready(function(){
                //$("#Q01").val("1");
                $("#form1 input[name='Q01']").val("1");
                //$("input[name='Q01']").val('1');
         });
      </script>
   </HEAD>
   <BODY>
      <form id="form1">
         <input type="text" id="Q01" name="Q01" maxlength="1" />
      </form>
   </BODY>
</HTML>

I expect, when I load the page, what will be in the input text 1. But the input text remains open. Any ideas?

- EDIT -

, , :

<!DOCTYPE HTML>
    <HTML>
       <HEAD>
          <script type="text/javascript" src="javascript/jquery-1.6.1.min.js"></script>
          <script type="text/javascript">
             $(document).ready(function(){
                    $("#Q01").val("1");
             });
          </script>
       </HEAD>
       <BODY>
          <form id="form1">
             <input type="text" id="Q01" name="Q01" maxlength="1" />
          </form>
       </BODY>
    </HTML>
+3
4

jQuery, id name , - :

$('#Q01').val('1')

val sou, : http://api.jquery.com/val/

+4

. jQuery . , .

, , - :

$(document).ready(function() {
    $("#form1 input[name='Q01']").val("1");
});

#form1 document.getElementById("form1"). input[name='Q01'] input, name, Q01. .val 1.

+3
$(document).ready(function(){
   $("form input[name='Q01']).val('1');         
)};
0
source

This should work:

 $("input[name='Q01']").val('1');

But why not set an id on your inputs?

Hope this works.

0
source

All Articles