Create a dynamic link based on checkbox values

What I'm trying to achieve is this:

  • Default state of page = no checkboxes ticked, no link shown
  • User ticks one (or more) checkboxes
  • Link appears, dynamically generated from the checkbox values, in the following format: http://example.com/?subject=Products&checked=Blue,Green,Purple (where the selected checkbox values are "Blue", "Green" and "Purple")
  • Thus far, based on advice from another question (Using JavaScript to load checkbox Values into a string), I've been able to get the values in the proper format (as part of the required url) printed to console.log via a button:

    $("button").on("click", function(){
    	var arr = []
    	$(":checkbox").each(function(){
    	   if($(this).is(":checked")){
    		 arr.push($(this).val())
    	   }
    	})
    	var vals = arr.join(",")
    	var str = "http://example.com/?subject=Products&" + vals
    	console.log(str)	
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <input type="checkbox" id="selected" name="selected" value="Blue" class="products"> Blue<br>
    <input type="checkbox" id="selected" name="selected" value="Green" class="products"> Green<br>
    <input type="checkbox" id="selected" name="selected" value="Purple" class="products"> Purple
    <br>
    <button>button</button>

    I believe that you were on the right track, if I understood your question correctly. I added the change event on you checkboxes as you suggested. Try the modified code below.

    HTML

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <input type="checkbox" name="selected" value="Blue" class="products"> Blue<br>
    <input type="checkbox" name="selected" value="Green" class="products"> Green<br>
    <input type="checkbox" name="selected" value="Purple" class="products"> Purple
    <br>
    <span class="link"></span>
    

    JavaScript

    $("input[type=checkbox]").on("change", function(){
        var arr = []
        $(":checkbox").each(function(){
           if($(this).is(":checked")){
             arr.push($(this).val())
           }
        })
        var vals = arr.join(",")
        var str = "http://example.com/?subject=Products&checked=" + vals
        console.log(str);
    
      if (vals.length > 0) {
        $('.link').html($('<a>', {
          href: str,
          text: str
        }));
      } else {
        $('.link').html('');
      }  
    })
    

    Working CodePen

    Your button is no longer being used. Is this what you were looking for?


    This would work to give you a url of

    http://example.com/?subject=Products&checked=Blue,Green,Purple

    (see below for a possibly better way):

    $(document).on("change", ".mod-link", function() {
      var arr = []
      $(".mod-link:checked").each(function() {
        arr.push($(this).val());
      })
      var vals = arr.join(",")
      var str = "http://example.com/?subject=Products&checked=" + vals;
      var link = arr.length > 0 ? '<a href="'+str+'">Click me</a>': '' ;
      
      $('.link-container').html(link);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" class="mod-link" name="selected" value="Blue" class="products">Blue
    <br>
    <input type="checkbox" class="mod-link" name="selected" value="Green" class="products">Green
    <br>
    <input type="checkbox" class="mod-link" name="selected" value="Purple" class="products">Purple
    <br>
    <div class="link-container"></div>
    链接地址: http://www.djcxy.com/p/56010.html

    上一篇: 如何在检查时提醒复选框的值

    下一篇: 根据复选框值创建一个动态链接