【问题标题】:jquery ReplaceWith not working with 2 buttonsjquery ReplaceWith 不能使用 2 个按钮
【发布时间】:2020-08-17 01:09:44
【问题描述】:

我创建了 2 个按钮,每个按钮将根据部分视图替换内容,当我单击按钮时,我可以在页面上加载部分视图,但这只能工作一次,例如我单击按钮 1 并加载数据,现在如果我点击按钮 2 它不起作用,我需要返回主页再次点击按钮 2

    <h3>
       <a class="btn btn-warning" id="button1"> Partial View 1</a>
    </h3> 
    <br/>
    <h4>
        <a class="btn btn-warning" id="buttton2"> Partial view 2</a>
    </h4>
   <br/> <br/>
    <div id="testsim">
    </div>




<script type="text/javascript">
$(function () {
    $('#button1').click(function () {
        $.get('@Url.Action("partialview1", "Home")', function (data1) {
            if (data1) {
                $('#testsim').replaceWith(data);
            }
        });
        });
         $('#button2').click(function () {
             $.get('@Url.Action("partialview2", "Home")', function (data2) {
                 if (data2) {
                     $('#testsim').replaceWith(data2);
                 }
        });
        });


});

</script>

我正在尝试实现按钮点击以在两个按钮之间切换,每次按钮点击都应该替换 div 标签中的内容。任何帮助将不胜感激。

【问题讨论】:

    标签: html jquery asp.net-mvc-5


    【解决方案1】:

    我认为问题在于replaceWith() 替换了元素本身,即outerHTML-

    $(function() {
      let $html, current;
      $('#button1').click(function() {
        /* $.get('@Url.Action("partialview1", "Home")', function(data1) {
           if (data1) {
             $('#testsim').replaceWith(data);
           }
         });*/
        current = `button 1 was <em>clicked</em>`;
        $html = `<div><strong>${current}</strong></div>`;
        $('#testsim').replaceWith($html);
      });
    
      $('#button2').click(function() {
        /*$.get('@Url.Action("partialview2", "Home")', function(data2) {
          if (data2) {
            $('#testsim').replaceWith(data2);
          }
        });*/
        current = `button 2 was <strong>clicked</strong>`;
        $html = `<div><em>${current}</em></div>`;
        $('#testsim').replaceWith($html);
      });
    
    
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <h3>
      <a class="btn btn-warning" id="button1"> Partial View 1</a>
    </h3>
    <br/>
    <h4>
      <a class="btn btn-warning" id="button2"> Partial view 2</a>
    </h4>
    <br/> <br/>
    <div id="testsim" style="background: aquamarine; height: 200px">
    </div>

    如您所见,替换后元素的样式消失了。如果你想执行这个操作,那么你应该使用html(),它只替换innerHTML-

    $(function() {
      let $html, current;
      $('#button1').click(function() {
        /* $.get('@Url.Action("partialview1", "Home")', function(data1) {
           if (data1) {
             $('#testsim').replaceWith(data);
           }
         });*/
        current = `button 1 was <em>clicked</em>`;
        $html = `<div><strong>${current}</strong></div>`;
        $('#testsim').html($html);
      });
    
      $('#button2').click(function() {
        /*$.get('@Url.Action("partialview2", "Home")', function(data2) {
          if (data2) {
            $('#testsim').replaceWith(data2);
          }
        });*/
        current = `button 2 was <strong>clicked</strong>`;
        $html = `<div><em>${current}</em></div>`;
        $('#testsim').html($html);
      });
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <h3>
      <a class="btn btn-warning" id="button1"> Partial View 1</a>
    </h3>
    <br/>
    <h4>
      <a class="btn btn-warning" id="button2"> Partial view 2</a>
    </h4>
    <br/> <br/>
    <div id="testsim" style="background: aquamarine; height: 200px">
    </div>

    希望这对您有所帮助。

    【讨论】:

    • 如何在 .html 中传递部分视图,而不是像“xyz”或“abc”这样的文本? (@Url.Action("Partialview", "Home")
    • @Maddy,通常的方式。我不知道您使用的是哪种语言或框架,但您通过AJAX 获取数据,从中创建一个HTML,将其存储在variable 中,然后显示它。我已将答案更新为显示 HTML 而不仅仅是 strings。如果问题仍然存在,请尝试使用相关的语言/框架更新问题的标签
    • 我已将代码更改为 Ajax,后来它与 .html() 配合得很好
    • 我很高兴,我能帮上忙。快乐编码。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    相关资源
    最近更新 更多