开发者

jquery hide input element error

The initial idea, is to hide an input element and show it based on a change event in a select element. However, the code I have is not hiding the input and so nothing happens with the change statement. Could someone check the code and show me my error. Thanks

<label for="AUSR_companyname">Company Name:</label>
<input id="AUSR_companyname" name="AUSR_companyname" type="text" class="text ui-widget-content ui-corner-all inputbox AUSR_companyname" value = "" />


!--- dropdown for new user addition  -->
<script language="javascript" type="text/javascript">
      $("#AUSR_companyname").hide();
            $(function() {
            jQuery("#AUSR_company").live('change', function() {
                  if($(this).val()=="new")
                 {
                    $("#AUSR_companyname").show(); 
                }
            });
      });
</script>
<!--- end of dropdown for new user addition -->

++++UPDATE++++

This script is now causing error 'missing ) after argument list'. Where have I missed it? Thanks

$(function() {
    $("#AUSR_companyname, label[for=AUSR_companyname]").hide();
            jQuery('#AUSR_company").live('change', function() {
                  if($(this).val()=="new")
                 {
                    $("#AUSR_company开发者_运维百科name").show(); 
                }
        });
      });


It looks like you are using jQuery. Take care that you have included the jQuery-files in the html header section.

After that, wrap your javascript code into the jQuery ready function:

<script language="javascript" type="text/javascript">
   $(document).ready(function() {
      $("#AUSR_companyname").hide();
        $(function() {
        jQuery("#AUSR_company").live('change', function() {
              if($(this).val()=="new")
             {
                $("#AUSR_companyname").show(); 
            }
        });
    });
  });
  </script>

EDIT: to answer your question you posted in your comment block: To hide the label, too you could use something like this:

$('label[for="AUSR_companyname"], #AUSR_companyname').hide();


Works for me.

http://jsfiddle.net/jasongennaro/9vVah/

Something else must be affecting the code

EDIT

In response to this comment you just added

There is one other thing. How do I hide a label along with the input field?

Do this

 $("#AUSR_companyname, label[for=AUSR_companyname]").hide();

http://jsfiddle.net/jasongennaro/9vVah/1/


I don't see any issues with the code, other than saying wrap everything under jQuery.ready block: http://jsfiddle.net/mrchief/7Rsce/


It's working for me,

I took the liberty to enclose the input and label in a div, and add the select: example

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜