联系表单中的隐藏字段

发布于 2024-11-02 02:53:03 字数 5198 浏览 2 评论 0原文

是否可以使用 jQuery 根据下拉菜单中的不同选择显示隐藏字段?

我在为多个隐藏字段编写代码时遇到问题。

基本上,我需要通过分配“hide3”或“hide4”来分配“select 1”中的所有选择,以显示同一字段集中的不同字段。

  <script type="text/javascript">
 $(document).ready(function(){
 $("#select1").change(function(){


    if ($(this).val() == "retouching" ) {

        $("#hide1").slideDown("fast"); 


    } else {

        $("#hide1").slideUp("fast");  

    }


});

$("#select2").change(function(){

    if ($(this).val() == "fashion" || $(this).val() == "beauty" || $(this).val() == "product" || $(this).val() == "architectural") {

        $("#hide2").slideDown("fast");

    } else {

        $("#hide1").slideUp("fast"); 


    }

});
$("#select1").change(function(){


    if ($(this).val() == "photography" ) {

        $("#hide3").slideDown("fast"); 


    } else {

        $("#hide3").slideUp("fast");  

    }


});

$("#select3").change(function(){

    if ($(this).val() == "fashion" || $(this).val() == "beauty" || $(this).val() == "product") {

        $("#hide4").slideDown("fast");

    } else {

        $("#hide3").slideUp("fast"); 


    }
});
    $("#select1").change(function(){


    if ($(this).val() == "studio" ) {

        $("#hide5").slideDown("fast"); 


    } else {

        $("#hide5").slideUp("fast");  

    }


});
});
</script>


  <fieldset>
         <legend>Project Details</legend>
          <div class="input select">

    <dl>
        <dt><label for="select1">Subject:</label></dt>
        <dd>
        <select name="select1" id="select1">
            <option value="">(choose one)</option>
            <option value="photography">Photography</option>
            <option value="retouching">Retouching</option>
            <option value="studio">Studio Rental</option>
        </select>
        </dd>
        </dl>
</div>

    <div class="hide" id="hide1">
        <div class="input select">

        <dl>
            <dt><label for="select2">Type:</label></dt>
            <dd>
            <select name="select2" id="select2">
                <option value="">(choose one)</option>
                <option value="fashion">Fashion</option>
                <option value="beauty">Beauty</option>
                <option value="product">Product</option>
                <option value="architectural">Architectural</option>
            </select>
            </dd>
        </dl>

</div>
</div>


    <div class="hide" id="hide2"> 
        <div class="input select">  

             <dl>
        <dt><label for="budget">Budget:</label></dt>
        <dd><input type="text" name="budget" id="budget" size="32" maxlength="128" /></dd>
    </dl>
    <dl>


        <dt><label for="message">Message:</label></dt>
        <dd><textarea name="comments" id="comments" rows="5" cols="60"></textarea></dd>
    </dl>
    <dl>
        <dt><label for="upload">Upload a File:</label></dt>
        <dd><input type="file" name="upload" id="upload" /></dd>
    </dl>
</div>
</div>



    <div class="hide" id="hide3">
        <div class="input select">

        <dl>
            <dt><label for="select3">Type:</label></dt>
            <dd>
            <select name="select3" id="select3">
                <option value="">(choose one)</option>
                <option value="fashion">Fashion</option>
                <option value="beauty">Beauty</option>
                <option value="product">Product</option>
            </select>
            </dd>

        </dl>
  </div>
  </div>


    <div class="hide" id="hide4"> 
        <div class="input select">
             <dl>
        <dt><label for="budget">Budget:</label></dt>
        <dd><input type="text" name="budget" id="budget" size="32" maxlength="128" /></dd>
    </dl>
    <dl>


        <dt><label for="message">Message:</label></dt>
        <dd><textarea name="comments" id="comments" rows="5" cols="60"></textarea></dd>
    </dl>
    <dl>
        <dt><label for="upload">Upload a File:</label></dt>
        <dd><input type="file" name="upload" id="upload" /></dd>
    </dl>
    </div>
    </div>


    <div class="hide" id="hide5">
        <div class="input select">

    <dl>
        <dt><label for="message">Message:</label></dt>
        <dd><textarea name="comments" id="comments" rows="5" cols="60"></textarea></dd>
    </dl>
    </div>
    </div>

</fieldset>

Is it possible to use jQuery to display hidden fields based on different selections from a dropdown menu?

I'm having trouble writing the code for more than one hidden field.

Basically, I need to assign all selections in "select 1" to show different fields in the same fieldset by assigning "hide3" or "hide4".

  <script type="text/javascript">
 $(document).ready(function(){
 $("#select1").change(function(){


    if ($(this).val() == "retouching" ) {

        $("#hide1").slideDown("fast"); 


    } else {

        $("#hide1").slideUp("fast");  

    }


});

$("#select2").change(function(){

    if ($(this).val() == "fashion" || $(this).val() == "beauty" || $(this).val() == "product" || $(this).val() == "architectural") {

        $("#hide2").slideDown("fast");

    } else {

        $("#hide1").slideUp("fast"); 


    }

});
$("#select1").change(function(){


    if ($(this).val() == "photography" ) {

        $("#hide3").slideDown("fast"); 


    } else {

        $("#hide3").slideUp("fast");  

    }


});

$("#select3").change(function(){

    if ($(this).val() == "fashion" || $(this).val() == "beauty" || $(this).val() == "product") {

        $("#hide4").slideDown("fast");

    } else {

        $("#hide3").slideUp("fast"); 


    }
});
    $("#select1").change(function(){


    if ($(this).val() == "studio" ) {

        $("#hide5").slideDown("fast"); 


    } else {

        $("#hide5").slideUp("fast");  

    }


});
});
</script>


  <fieldset>
         <legend>Project Details</legend>
          <div class="input select">

    <dl>
        <dt><label for="select1">Subject:</label></dt>
        <dd>
        <select name="select1" id="select1">
            <option value="">(choose one)</option>
            <option value="photography">Photography</option>
            <option value="retouching">Retouching</option>
            <option value="studio">Studio Rental</option>
        </select>
        </dd>
        </dl>
</div>

    <div class="hide" id="hide1">
        <div class="input select">

        <dl>
            <dt><label for="select2">Type:</label></dt>
            <dd>
            <select name="select2" id="select2">
                <option value="">(choose one)</option>
                <option value="fashion">Fashion</option>
                <option value="beauty">Beauty</option>
                <option value="product">Product</option>
                <option value="architectural">Architectural</option>
            </select>
            </dd>
        </dl>

</div>
</div>


    <div class="hide" id="hide2"> 
        <div class="input select">  

             <dl>
        <dt><label for="budget">Budget:</label></dt>
        <dd><input type="text" name="budget" id="budget" size="32" maxlength="128" /></dd>
    </dl>
    <dl>


        <dt><label for="message">Message:</label></dt>
        <dd><textarea name="comments" id="comments" rows="5" cols="60"></textarea></dd>
    </dl>
    <dl>
        <dt><label for="upload">Upload a File:</label></dt>
        <dd><input type="file" name="upload" id="upload" /></dd>
    </dl>
</div>
</div>



    <div class="hide" id="hide3">
        <div class="input select">

        <dl>
            <dt><label for="select3">Type:</label></dt>
            <dd>
            <select name="select3" id="select3">
                <option value="">(choose one)</option>
                <option value="fashion">Fashion</option>
                <option value="beauty">Beauty</option>
                <option value="product">Product</option>
            </select>
            </dd>

        </dl>
  </div>
  </div>


    <div class="hide" id="hide4"> 
        <div class="input select">
             <dl>
        <dt><label for="budget">Budget:</label></dt>
        <dd><input type="text" name="budget" id="budget" size="32" maxlength="128" /></dd>
    </dl>
    <dl>


        <dt><label for="message">Message:</label></dt>
        <dd><textarea name="comments" id="comments" rows="5" cols="60"></textarea></dd>
    </dl>
    <dl>
        <dt><label for="upload">Upload a File:</label></dt>
        <dd><input type="file" name="upload" id="upload" /></dd>
    </dl>
    </div>
    </div>


    <div class="hide" id="hide5">
        <div class="input select">

    <dl>
        <dt><label for="message">Message:</label></dt>
        <dd><textarea name="comments" id="comments" rows="5" cols="60"></textarea></dd>
    </dl>
    </div>
    </div>

</fieldset>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白衬杉格子梦 2024-11-09 02:53:03

此行不是有效的 Javascript:

if ($(this).val() == "fashion", "beauty", "product" )

您可以将其更改为:

if ($(this).val() == "fashion" || $(this).val() == "beauty" || $(this).val() == "product")

或者(在我看来更好)其他方式:

switch ($(this).val())
{
    case "fashion":
        // do stuff
        break;
    case "beauty":
        // do stuff
        break;        
    case "product":
        // do stuff
        break;
}

This line is not valid Javascript:

if ($(this).val() == "fashion", "beauty", "product" )

You can change that to:

if ($(this).val() == "fashion" || $(this).val() == "beauty" || $(this).val() == "product")

Or, the (better, in my opinion) other way:

switch ($(this).val())
{
    case "fashion":
        // do stuff
        break;
    case "beauty":
        // do stuff
        break;        
    case "product":
        // do stuff
        break;
}
昇り龍 2024-11-09 02:53:03

如果你的来源是准确的,那么你对结束标签的处理非常草率——检查是否缺少 ,我希望这(和吉米的评论)能够使它工作。

If your source is accurate you are very sloppy with closing tags -- check for missing </div>, I expect this will (with Jimmy's comment) make it work.

不即不离 2024-11-09 02:53:03

为什么要隐藏字段,选择的时候就创建,简单

Why do you want to hide the fields, create it when the selection is made, simple

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文