jQuery如果x复选框选中更改链接

时间:2017-09-27 13:30:55

标签: jquery checkbox

我需要一个jQuery脚本。我有三个chechboxes和两个链接。如果选中所有三个复选框,则会显示链接1.如果仅选中1或2个复选框,则会显示链接2.



function toggleLink(checkBox){
  var link = document.getElementById("agreeLink");
  if (checkBox.checked)
    link.style.display = "inline";
  else
    link.style.display = "none";

  var link = document.getElementById("dontagreeLink");

  if (checkBox.checked)
    link.style.display = "none";
  else
  link.style.display = "inline";
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <section title=".agreeCheckbox">
    <div class="agreeCheckbox">
      <input type="checkbox" value="None" id="agreeCheckbox1" name="agreeCheckbox" unchecked onchange="toggleLink(this);"/>
      <label for="agreeCheckbox"></label>
    </div>
  </section>
</div> 

<div style="padding-top:100px;">
  <section title=".agreeCheckbox">
    <div class="agreeCheckbox">
      <input type="checkbox" value="None" id="agreeCheckbox" name="agreeCheckbox" unchecked onchange="toggleLink(this);"/>
      <label for="agreeCheckbox"></label>
    </div>
  </section>
</div> 

<div style="padding-top:100px;">
  <section title=".agreeCheckbox">
    <div class="agreeCheckbox">
      <input type="checkbox" value="None" id="agreeCheckbox" name="agreeCheckbox" unchecked onchange="toggleLink(this);"/>
      <label for="agreeCheckbox"></label>
    </div>
  </section>
</div>  

<p>
  <a href="exmaple.com" id="agreeLink" style="display:none;">Jetzt bewerben!</a>
  <a href="none.com" id="dontagreeLink" style="display:inline;">Jetzt bewerben!</a>
</p>
&#13;
&#13;
&#13;

提前致谢。

2 个答案:

答案 0 :(得分:0)

每个复选框都需要name属性:

if ($('input[name="checkbox1"]:checked') && $('input[name="checkbox2"]:checked') && $('input[name="checkbox3"]:checked')) {
                //do stuff
            }

答案 1 :(得分:0)

您可以使用解决方案

&#13;
&#13;
Sub Test()

    Dim dict As Object
    Dim wrkBk As Workbook
    Dim vItem As Variant
    Dim sPath As String

    'All workbooks to open will be in this folder.
    'Remember to include the final back-slash (\).
    sPath = "C:\Test\"

    Set dict = CreateObject("Scripting.Dictionary")

    dict.CompareMode = vbTextCompare

    'If files will not all be in the same folder, then
    'full file path must be included here and remove
    'references to sPath variable in the code.
    dict.Add "Book2.xlsx", "Sheet1"
    dict.Add "Book3.xlsx", "Sheet2"

    For Each vItem In dict
        Set wrkBk = Workbooks.Open(sPath & vItem)
        With wrkBk.Worksheets("Sheet1")
            .Range(.Cells(2, 1), .Cells(.Rows.Count, 11).End(xlUp)).Copy
            ThisWorkbook.Worksheets(dict(wrkBk.Name)).Range("A2").PasteSpecial xlPasteValues
        End With
        wrkBk.Close SaveChanges:=False
    Next vItem

End Sub
&#13;
$('input[type="checkbox"]').change(function(){

  var checkboxCnt = 0;
  $('input[type="checkbox"]').each(function(){
    if($(this).is(':checked'))
      checkboxCnt++;
  });
  
  $('p').find('a').hide();

  if(checkboxCnt != 3){
    $('p').find('a#agreeLink').show();
  } else {
    $('p').find('a#dontagreeLink').show(); 
  } 
});
&#13;
&#13;
&#13;

希望这会对你有所帮助

相关问题