jquery .hide / show form仍然将数据发送到控制器

时间:2015-09-21 13:25:47

标签: jquery

很简单,如果#parentPermission被隐藏,它会将(parentPermission)的post变量发送到控制器,有两个变量可以通过将id = link发送到控制器来工作,如果一个被隐藏则它不应该发送数据,但通常会发生什么,它不能识别html选择#link形式。

代码:

$(document).ready(function () {
    toggleFields(); 
    $("#link").click(function () {
        toggleFields();
    });
});

//this toggles the visibility of our parent permission fields depending on the current selected value of the underAge field

function toggleFields() {
    if ($("#link").val() == 'other')
        $("#parentPermission").show();
    else
        $("#parentPermission").hide();
}

HTML mvc查看:

    <!-- Select Basic -->
    <div class="form-group">
      <label class="col-md-4 control-label" for="assign_article">Assign Article to Menu</label>
      <div class="col-md-4">
        <select id="link" name="link" class="form-control">

      <?php 

                foreach($get_data as $ShowMenu) { ?>

          <option value="<?php echo $ShowMenu['id']; ?>"><?php  echo $ShowMenu['title']; ?></option>


        <?php } ?>


        <option id="parent" value="parent">Other</option>

        </select>






      </div>
          </div>



  <!-- Text input-->
    <div id="parentPermission" class="form-group">
      <label class="col-md-4 control-label" for="menu_name">Other:</label>
      <div id="parentPermission" class="col-md-4">
        <input id="parentPermission" name="link" type="text" placeholder="menu name" class="form-control input-md">
        </div>
    </div>

       <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
    toggleFields(); //call this first so we start out with the correct visibility depending on the selected form values
    //this will call our toggleFields function every time the selection value of our underAge field changes
    $("#link").change(function () {
        toggleFields();
    });

});
//this toggles the visibility of our parent permission fields depending on the current selected value of the underAge field
function toggleFields() {
    if ($("#link").val() == 'parent')
        $("#parentPermission").show();

    else
        $("#parentPermission").hide();
}
</script>

    <!-- Select Basic -->

1 个答案:

答案 0 :(得分:0)

我使用php中的if语句解决了这个问题但是jquery很难不使用.remove函数发送数据