【问题标题】:Reload only <Div> not whole form仅重新加载 <Div> 而不是整个表单
【发布时间】:2015-09-09 00:30:04
【问题描述】:

请看一下这段代码。我只想刷新表单的一部分而不是整个页面。当前代码正在刷新整个页面。由于所有条目都在数据库中,因此无需查找数据库错误

<script>
        $(document).ready(function() {
      $('#refbutton').click(function() {
        var val = $('#refinfo').val();
        $.ajax({
          type: 'POST',
          url: 'selectbox.php',
          data: 'val=' + val,
          success: function(html) {
            alert(html);
            window.location.reload();
          }
        });
      });
    });
    </script>

HTML 代码

    <form>
    <table>
    <tr>
            <td >Name</td>
            <td ><input name="name" type="text" /></td>
        </tr>

        <tr>
            <td>Address</td>
            <td><textarea name="address" ></textarea></td>
        </tr>
<div id="mydiv">
    <tr>
            <td>Reference</td>

            <td>

                <select data-placeholder="Choose a reference..." class="chosen-select" name ="reference1" style="width:350px;" tabindex="2">
                <option value="default">Please Select</option>
                <?php

                $sql="select * from reference ";
                //echo $sql;
                $tbl=mysql_query($sql);
                while($row=mysql_fetch_array($tbl))
                {
                ?>
                <option value="<?php echo $row['reference'];?>"><?php echo $row['reference'];?></option>
                <?php }?>


              </select>

            </td>

            <td>
                <input name="reference2" type="text" id="refinfo" ></input>
            </td>
            <td>
                <input name="Button_ref" id="refbutton" type="button" value="Add Reference"></td>

        </tr>
</div>
    </table>
    </form>

【问题讨论】:

  • 删除window.location.reload();并使用$('div').html(html)
  • @ShaunakD 这样我的条目就不会被添加到选择框中
  • 将值附加到选择框而不是重新加载页面。
  • @lshettyl 我想将 reference2 中的值添加到 db 中,然后在选择框中从 db 中获取值
  • 兄弟我想你应该看看JavaScript中的同步和异步是什么?截至目前。

标签: javascript jquery html ajax dom


【解决方案1】:

您的 HTML 无效,因为在 TRs 之间不能有 &lt;div id="mydiv"&gt;。为什么你甚至想要它?如果你想要一个 div,你必须把它放在 TDTABLE 之外。我认为,您想要的只是添加到数据库中的新选项,将其附加到选择框并使用返回的html 填充&lt;div id="mydiv"&gt;。试试下面的代码。

$(document).ready(function() {
    $('#refbutton').click(function() {
        var val = $('#refinfo').val();
        var $select = $(".chosen-select");

        $.ajax({
            type: 'POST',
            url: 'selectbox.php',
            data: { val: val },
            success: function(html) {
                $("#mydiv").html(html);
                //This would append the new option to the select box without page reload
                $select.append('<option value="' + val + '">' + val + '</option>');
            }
        });
    });
});

这里是a fiddle,您的 HTML 已更正。我已将&lt;div id="mydiv"&gt; 放在表单之前。

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    HTML:

    <form>
        <table>
        <tr>
                <td >Name</td>
                <td ><input name="name" type="text" /></td>
            </tr>
    
            <tr>
                <td>Address</td>
                <td><textarea name="address" ></textarea></td>
            </tr>
    <div id="mydiv">
        <tr>
                <td>Reference</td>
    
                <td>
                    <div class="my-div">NEW HTML GOES HERE</div>
    
                    <select data-placeholder="Choose a reference..." class="chosen-select" name ="reference1" style="width:350px;" tabindex="2">
                    <option value="default">Please Select</option>
                    <?php
    
                    $sql="select * from reference ";
                    //echo $sql;
                    $tbl=mysql_query($sql);
                    while($row=mysql_fetch_array($tbl))
                    {
                    ?>
                    <option value="<?php echo $row['reference'];?>"><?php echo $row['reference'];?></option>
                    <?php }?>
    
    
                  </select>
    
                </td>
    
                <td>
                    <input name="reference2" type="text" id="refinfo" ></input>
                </td>
                <td>
                    <input name="Button_ref" id="refbutton" type="button" value="Add Reference"></td>
    
            </tr>
    </div>
        </table>
        </form>
    

    JavaScript:

    <script>
            $(document).ready(function() {
          $('#refbutton').click(function() {
            var val = $('#refinfo').val();
            $.ajax({
              type: 'POST',
              url: 'selectbox.php',
              data: 'val=' + val,
              success: function(html) {
                $('.my-div').html(html);
              }
            });
          });
        });
        </script>
    

    【讨论】:

    • 这只会显示 html 的值,我必须手动重新加载整个页面才能在选择框中显示数据,这当然会删除在其他文本框中输入的文本
    • @swinn 如果您想更改 &lt;div&gt;&lt;select&gt; 中的代码,则使用相同的 jQuery 代码。请不要指望我会写你的申请..
    猜你喜欢
    • 2013-09-18
    • 2016-04-19
    • 1970-01-01
    • 2013-10-28
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2014-09-06
    相关资源
    最近更新 更多