如何在每次点击按钮时更改按钮的文本值?

问题描述:

下面我有jquery代码和表。 jQuery代码是在每次单击回复按钮时切换名为“divrep”的div元素。我想要的是我希望回复按钮在每次点击时改变文本值。 所以最初,按钮的文本值是“Replie(s)”。我想在单击时将其更改为“隐藏回复”,并在再次单击时再次回到“复制”。如何在我的代码上添加脚本来执行此操作?谢谢...如何在每次点击按钮时更改按钮的文本值?

的Jquery:

$(function() { 
    $('.Reply').click(function() { 
    $(this).closest('p').next('.divrep').toggle(!$(this).closest('p').next('.divrep').is(":visible")); 
    // How to insert a code here to change the text value of the reply button in every click? 
}); 
}); 

HTML:

<table id="mytable"> 

    <tr > 
     <td class="tdstyle" > 

    <div style="font-weight:bold;"> @Html.DisplayFor(modelItem => item.name) </div> 

    <p class="comment more" style ="white-space: pre-line; margin-top:0px;margin-bottom:0px; border-radius: 4px 4px 4px 4px; max-width :500px; min-height :5px; display :block; background-color: #CCCCFF"> @Html.DisplayFor(modelItem => item.comment) </p> 
    <p> <input type="button" id="like" name="like" value="Like" style="color:blue;border:0px;background-color:inherit;cursor:pointer" /> <input type="button" class ="Reply" name="Reply" value="Replie(s)" style="margin-bottom:0px;color:blue;border:0px;background-color:inherit;cursor:pointer" /></p>                                 
    <div id="divReply" class ="divrep" style="display:none; position:relative;left:50px; overflow:auto;margin-top:0px;margin-bottom:0px"> 
      <table> 

       <tr > 

        <td > 
         <div style="font-weight:bold;"> @Html.DisplayFor(modelItem => item2.name) </div> 

        <p class="comment more" style ="margin-top:0px;margin-bottom:0px;white-space:pre-line; border-radius: 4px 4px 4px 4px; max-width :445px; min-height :5px; display :block; background-color: #CCCCFF;">@Html.DisplayFor(modelItem => item2.reply) </p> 

        </td> 
       </tr> 

      </table>  

    <div> 
     <div class="editor-field" style="display:none; margin-bottom:5px;margin-top:5px"> 
      <input type="text" id="comidvalue" name="id" class="id" value="@Html.DisplayFor(modelItem => item.Id)" /> 
     </div> 

     <br /> 
     <input type="text" id="namerep" name="name" class="name" style="width:445px;resize:none" /> 

     <br /> 
     <textarea id="reply" name="reply" class="reply" style="width:445px;height:100px;resize:none" ></textarea> 

     <br /> 

     <input type="button" class="postrep" value="Post Reply" name="butname" style="cursor:pointer" /> 

    </div> 
      <br /> 

    </div> 
     </td>  
    </tr> 


</table> 
+0

什么有关[小提琴](http://jsfiddle.net/)? – Manwal 2014-10-10 03:47:42

+0

如果你只有一个回复按钮,你不能只采取回复按钮的ID?而不是上课? – Dhwani 2014-10-10 03:49:32

+0

@DH__我无法使用该ID,因为我忘记告诉你,表内有循环,这就是为什么我使用了类。你有什么代码来添加它? – timmack 2014-10-10 04:02:51

您可以尝试使用的可见性状态设置文本值divrep

$(function() { 
 
    $('.Reply').click(function() { 
 
     var $divrep = $(this).closest('p').next('.divrep').toggle(!$(this).closest('p').next('.divrep').is(":visible")); 
 
     $(this).val($divrep.is(':visible') ? 'Hide Replie(s)' : 'Replie(s)') 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<table id="mytable"> 
 
    
 
    <tr > 
 
     <td class="tdstyle" > 
 
      
 
      <div style="font-weight:bold;"> @Html.DisplayFor(modelItem => item.name) </div> 
 
      
 
      <p class="comment more" style ="white-space: pre-line; margin-top:0px;margin-bottom:0px; border-radius: 4px 4px 4px 4px; max-width :500px; min-height :5px; display :block; background-color: #CCCCFF"> @Html.DisplayFor(modelItem => item.comment) </p> 
 
      <p> <input type="button" id="like" name="like" value="Like" style="color:blue;border:0px;background-color:inherit;cursor:pointer" /> <input type="button" class ="Reply" name="Reply" value="Replie(s)" style="margin-bottom:0px;color:blue;border:0px;background-color:inherit;cursor:pointer" /></p>                                 
 
      <div id="divReply" class ="divrep" style="display:none; position:relative;left:50px; overflow:auto;margin-top:0px;margin-bottom:0px"> 
 
       <table> 
 
        
 
        <tr > 
 
         
 
         <td > 
 
          <div style="font-weight:bold;"> @Html.DisplayFor(modelItem => item2.name) </div> 
 
          
 
          <p class="comment more" style ="margin-top:0px;margin-bottom:0px;white-space:pre-line; border-radius: 4px 4px 4px 4px; max-width :445px; min-height :5px; display :block; background-color: #CCCCFF;">@Html.DisplayFor(modelItem => item2.reply) </p> 
 
          
 
         </td> 
 
        </tr> 
 
        
 
       </table>  
 
       
 
       <div> 
 
        <div class="editor-field" style="display:none; margin-bottom:5px;margin-top:5px"> 
 
         <input type="text" id="comidvalue" name="id" class="id" value="@Html.DisplayFor(modelItem => item.Id)" /> 
 
        </div> 
 
        
 
        <br /> 
 
        <input type="text" id="namerep" name="name" class="name" style="width:445px;resize:none" /> 
 
        
 
        <br /> 
 
        <textarea id="reply" name="reply" class="reply" style="width:445px;height:100px;resize:none" ></textarea> 
 
        
 
        <br /> 
 
        
 
        <input type="button" class="postrep" value="Post Reply" name="butname" style="cursor:pointer" /> 
 
        
 
       </div> 
 
       <br /> 
 
       
 
      </div> 
 
     </td>  
 
    </tr> 
 
</table>

+0

这不会更改class =“Reply”按钮的文本值。你为什么使用divrep? divrep是要切换的div元素,但这不是我关心的问题。我担心的是要改变回复按钮的值。 – timmack 2014-10-10 03:58:58

+0

@timmack我以为'Reply'是一个锚元素....因为它是'输入'使用'.val()' – 2014-10-10 04:04:59

+0

谢谢你johnny。它正在工作。之所以没有在第一篇文章中工作,是因为你忘了添加这个var $ divrep – timmack 2014-10-10 04:11:45

对于<input>按钮,就可以传递给.val()方法来检查当前值,并返回新值的函数来进行相应的设置:

$(".Reply").val(function(i, value){ 
    return (value=="Replie(s)") ? "Hide Replie(s)" : "Replie(s)" 
}); 

对于<button>元素,可以使用.text()方法做同样的事情。

+0

我试过了,但那不会返回正在设置的文本值。但是这一个将会,$(this).val($ divrep.is(':visible')?'隐藏Replie(s)':'Replie(s)') – timmack 2014-10-10 04:17:37

+0

对不起,我没有注意到它是一个' '按钮,尝试更新... – 2014-10-10 04:19:09

+0

真棒,谢谢... – timmack 2014-10-10 04:24:14

只需添加到您的JS(而不是你的//注释行):

$(this).val($(this).val() == "Replie(s)" ? "Hide Replies" : "Replie(s)"); 

http://jsfiddle.net/the_julo/tnx5gd6u/2/

+0

只注意到我的答案只是T J的简写。 如果对某人有用,我会留下它。 – JuLo 2014-10-10 04:35:19