开发者

How to modify the value of text input with js/jquery?

I would like to dynamically modify the values of certain form elements, more specifically certain input开发者_开发技巧 text fields. So far, whenever I load my html page, I am just getting the blank input field, but I am expecting it to contain the value of 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 value of form could be different every time. Am I even approaching this correctly? Suggestions on how I can achieve my intended functionality?

-- EDIT --

None of the solutions seem to be doing the trick. Here is an update of 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 am expecting when I load the page, that the input text will have 1 in it. But the input text keeps showing up empty. Any ideas?

-- EDIT --

Here is a solution derived from the answers from below that I like:

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


If you are using jQuery you could just change the id attribute to name in the input elements and then do something like this:

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

The val method sets the value sou can find more here: http://api.jquery.com/val/


You've got your ready handler correct. One of the great things about jQuery is its selector engine. I recommend taking a look at the documentation to help familiarize yourself with it.

To do what you want, I'd recommend something like this:

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

The #form1 is the same as document.getElementById("form1"). The input[name='Q01'] grabs all input elements that have a name attribute equal to Q01. The .val method sets the selected elements value to the string 1.


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


This should work:

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

But why not to set id's to your inputs?

Hope it works

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜