在onclick上更改多个按钮文本

时间:2017-11-16 07:07:12

标签: jquery twitter-bootstrap

我试图在按钮文本点击此处时切换按钮文本。我为另一个按钮使用了不同的ID,但它似乎没有用。 如何为多个按钮使用相同的功能?



  function buttonToggle() {
                var change = document.getElementById("btnAdd");
                if (change.innerHTML == "ADD")
                {
                    change.innerHTML = "ADDED"
                }
                else {
                    change.innerHTML = "ADD"
                }
            }

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/register-dev.css"> 
    <link rel="stylesheet" href="../../css/activity-registration.css">

      
       <!-- jQuery first, then Tether, then Bootstrap JS. -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  

  <div class="col">
      <button type="button" class="btn btn-primary btn-lg" id="btnAdd" onclick="buttonToggle()">
     <span><i class="fa fa-plus" aria-hidden="true"></i> <b>ADD</b></span>
       </button>
        </div>

<br>
<br>

<div class="col">
		 <button type="button" class="btn btn-primary btn-lg" id="btnAdd3" onclick="buttonToggle()">
	     	<span><i class="fa fa-plus" aria-hidden="true"></i></span> <b>ADD</b>
	   </button>
       </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

检查字符串是不好的。如果文本更改为“添加”或“添加”或“添加”,该怎么办?因此,我总是建议使用类,因此addClassremoveClasshasClass。请查看以下代码段:

$("button").on("click", function() {
  if ($(this).find("b").hasClass('active')) {
    $(this).find("b").text("ADD").removeClass('active');
  } else {
    $(this).find("b").text("ADDED").addClass('active');
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>



<div class="col">
  <button type="button" class="btn btn-primary btn-lg" id="btnAdd">
     <span><i class="fa fa-plus" aria-hidden="true"></i> <b>ADD</b></span>
       </button>
</div>

<br>
<br>

<div class="col">
  <button type="button" class="btn btn-primary btn-lg" id="btnAdd3">
	     	<span><i class="fa fa-plus" aria-hidden="true"></i></span> <b>ADD</b>
	   </button>
</div>

答案 1 :(得分:0)

在切换功能中传递参数

 function buttonToggle(ButtonID) {
            var change = document.getElementById(ButtonID);
            if (change.innerHTML == "ADD")
            {
                change.innerHTML = "ADDED"
            }
            else {
                change.innerHTML = "ADD"
            }
        }

并称之为

对于按钮一:

<button type="button" class="btn btn-primary btn-lg" id="btnAdd" onclick="buttonToggle('btnAdd')">

对于按钮2:

<button type="button" class="btn btn-primary btn-lg" id="btnAdd2" onclick="buttonToggle('btnAdd2')">

function buttonToggle(ButtonID) {
            var change = document.getElementById(ButtonID);
            if (change.innerHTML == "ADD")
            {
                change.innerHTML = "ADDED"
            }
            else {
                change.innerHTML = "ADD"
            }
        }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/register-dev.css"> 
    <link rel="stylesheet" href="../../css/activity-registration.css">

      
       <!-- jQuery first, then Tether, then Bootstrap JS. -->
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  

  <div class="col">
      <button type="button" class="btn btn-primary btn-lg" id="btnAdd" onclick="buttonToggle('btnAdd')">
     <span><i class="fa fa-plus" aria-hidden="true"></i> <b>ADD</b></span>
       </button>
        </div>

<br>
<br>

<div class="col">
		 <button type="button" class="btn btn-primary btn-lg" id="btnAdd2" onclick="buttonToggle('btnAdd2')">
	     	<span><i class="fa fa-plus" aria-hidden="true"></i></span> <b>ADD</b>
	   </button>
       </div>