du4629 2015-09-18 19:35
浏览 47
已采纳

jQuery多选show show不适用于所有选择元素

To keep it simple I have 2 form multiple select elements. Depending on the dropdown selected, it will auto hide or show additional input fields.

This is achieved like such:

$(document).ready(function(){
    $("#find-replace1 select").change(function(){
        $( "select option:selected").each(function(){
            if($(this).attr("value")=="Find/Replace"){
                $(".rep1").hide();
                $(".find1").show();
                $(".replace1").show();
            } else {
                $(".rep1").show();
                $(".find1").hide();
                $(".replace1").hide();                 
            }
        });
    }).change();
});

$(document).ready(function(){
    $("#find-replace2 select").change(function(){
        $( "select option:selected").each(function(){
            if($(this).attr("value")=="Find/Replace"){
                $(".rep2").hide();
                $(".find2").show();
                $(".replace2").show();
            } else {
                $(".rep2").show();
                $(".find2").hide();
                $(".replace2").hide();                 
            }
        });
    }).change();
});

My html is:

<p>
<form method="POST" action="#" accept-charset="UTF-8">
    <div id="find-replace1" class="form-group-a">
        <select class="form-control" name="priceaction">
            <option value="Append">Append</option>
            <option value="Prepend">Prepend</option>
            <option value="Replace">Replace</option>
            <option value="Find/Replace">Find/Replace</option>
        </select>
    </div>
    <div class="form-group-a">
        <input class="form-control formwidth rep1" autocomplete="off" name="value" type="text" style="display: block;">
    </div>
    <div class="form-group-a">
        <input class="form-control formwidth find1" autocomplete="off" placeholder="find this" name="find" type="text" style="display: none;">
    </div>                 
    <div class="form-group-a">
        <input class="form-control formwidth replace1" autocomplete="off" placeholder="replace with" name="replace" type="text" style="display: none;">
    </div>                                      
    <div class="clear"></div>

    <input type="submit" value="Update">
</form>
</p>
<hr>
<p>
<form method="POST" action="#" accept-charset="UTF-8">
    <div id="find-replace2" class="form-group-a">
        <select class="form-control" name="priceaction">
            <option value="Append">Append</option>
            <option value="Prepend">Prepend</option>
            <option value="Replace">Replace</option>
            <option value="Find/Replace">Find/Replace</option>
        </select>
    </div>
    <div class="form-group-a">
        <input class="form-control formwidth rep2" autocomplete="off" name="value" type="text" style="display: block;">
    </div>
    <div class="form-group-a">
        <input class="form-control formwidth find2" autocomplete="off" placeholder="find this" name="find" type="text" style="display: none;">
    </div>                 
    <div class="form-group-a">
        <input class="form-control formwidth replace2" autocomplete="off" placeholder="replace with" name="replace" type="text" style="display: none;">
    </div>                                      
    <div class="clear"></div>

    <input type="submit" value="Update">
</form>
</p>

Here is the jsfiddle: https://jsfiddle.net/kqjkprc1/

On my live site I actually have more than one form element but I kept it simple with 2 for this example. I notice it only applies the jQuery to the last form element on the page no matter what. Not sure why the first form element is unaffected and won't show/hide the hidden input fields.

  • 写回答

1条回答 默认 最新

  • dongnao2048 2015-09-18 20:39
    关注

    On the one hand you can add different form actions like this

    <form method="POST" action="update_1.php" >
    </form>
    
    <form method="POST" action="update_2.php">
    </form>
    

    on the other hand you can define differnt submit names

    <form method="POST" action="#" >
     <input type="submit" name="form1">
    </form>
    
    <form method="POST" action="#" >
     <input type="submit" name="form2">
    </form>
    

    and get in in the php file like that

    if (!empty($_POST['form1'])) {
     //do something here for form 1;
    }
    if (!empty($_POST['form2'])) {
     //do something here for form 2;
    }
    

    --- UPDATE ---

    ahh ok i think i have a solution for you (first line changes)

    $( "option:selected", this).each(function(){
            if($(this).attr("value")=="Find/Replace"){
                $(".rep1").hide();
                $(".find1").show();
                $(".replace1").show();
            } else {
                $(".rep1").show();
                $(".find1").hide();
                $(".replace1").hide();                 
            }
        }); 
    

    https://jsfiddle.net/kqjkprc1/1/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号