简体   繁体   English

使用点击功能将参数添加到 URL

[英]Add paramters to URL with click functions

I want to add parameters to the actual url depending on the selected values.我想根据所选值将参数添加到实际 url。 The values will be set with a click function and should work independently from each other.这些值将通过点击功能进行设置,并且应该彼此独立工作。

I've set global variables to store the click function values, but they don't get stored.我已经设置了全局变量来存储点击函数值,但它们没有被存储。 How can I store the values correctely to work with them in my "URLSearchParams" string?如何正确存储这些值以在我的“URLSearchParams”字符串中使用它们?

This is the Fiddle .这是小提琴

 var btn0;    
 var btn1;
      
      $('.btn0').click(function () {
    if ($(this).attr('data-selected') === 'true') {
      $(this).attr('data-selected', 'false');
      $(this).removeClass('selected');

    } else {
      $(this).closest('tr').find('.btn0').not(this)
        .removeClass('selected').attr('data-selected', 'false');
      $(this).attr('data-selected', 'true');
      $(this).addClass('selected');
      var btn0 = $(this).data("value");
    }
  });
  
      $('.btn1').click(function () {
        if ($(this).attr('data-selected') === 'true') {
            $(this).attr('data-selected', 'false');
            $(this).removeClass('selected');

        } else {
            $(this).closest('tr').find('.btn1').not(this)
            .removeClass('selected').attr('data-selected', 'false');
            $(this).attr('data-selected', 'true');
            $(this).addClass('selected');
            var btn1 = $(this).data("value");
        }
    });
    
const params = new URLSearchParams({
  var0: btn0,
  var1: btn1
});   
console.log(params.toString());

The params object does not reference to variables, but is initialized with empty btn0 and btn1. params 对象不引用变量,而是用空的 btn0 和 btn1 进行初始化。 Re declaring btn0 and btn1 inside the click assignments makes no sense, because these will be new variables.在点击赋值中重新声明 btn0 和 btn1 是没有意义的,因为它们将是新变量。 You have to use the set function to manipulate key value pairs in params.您必须使用 set 函数来操作参数中的键值对。

Here is a working example:这是一个工作示例:

 var btn0; var btn1; $('.btn0').click(function () { if ($(this).attr('data-selected') === 'true') { $(this).attr('data-selected', 'false'); $(this).removeClass('selected'); } else { $(this).closest('tr').find('.btn0').not(this) .removeClass('selected').attr('data-selected', 'false'); $(this).attr('data-selected', 'true'); $(this).addClass('selected'); params.set('var0', $(this).data("value")); console.log(params.toString()); } }); $('.btn1').click(function () { if ($(this).attr('data-selected') === 'true') { $(this).attr('data-selected', 'false'); $(this).removeClass('selected'); } else { $(this).closest('tr').find('.btn1').not(this) .removeClass('selected').attr('data-selected', 'false'); $(this).attr('data-selected', 'true'); $(this).addClass('selected'); params.set('var1', $(this).data("value")); console.log(params.toString()); } }); const params = new URLSearchParams({ var0: btn0, var1: btn1 }); console.log(params.toString());

Consider the following: https://jsfiddle.net/Twisty/ckd1j6u0/考虑以下内容: https ://jsfiddle.net/Twisty/ckd1j6u0/

HTML HTML

<table class="container">
  <tbody>
    <tr class="tier0">
      <td class="talent-cell">
        <div class="btn btn0" data-value="a">Test0</div>
      </td>
      <td class="talent-cell">
        <div class="btn btn0" data-value="b">Test1</div>
      </td>
      <td class="talent-cell">
        <div class="btn btn0" data-value="c">Test2</div>
      </td>
    </tr>
    <tr class="tier1">
      <td class="talent-cell">
        <div class="btn btn1" data-value="d">Test0</div>
      </td>
      <td class="talent-cell">
        <div class="btn btn1" data-value="e">Test1</div>
      </td>
      <td class="talent-cell">
        <div class="btn btn1" data-value="f">Test2</div>
      </td>
    </tr>
  </tbody>
</table>

JavaScript JavaScript

$(function() {
  var selected = {
    var0: null,
    var1: null
  };
  $('.btn').click(function(event) {
    var row = $(this).closest("tr");
    $(".selected", row).removeClass("selected");
    $(this).addClass("selected");
    if ($(".selected").length == 2) {
      $("tr").each(function(i, el) {
        selected['var' + i] = $(".selected", el).attr("data-value");
      });
      var params = new URLSearchParams(selected);
      console.log(params.toString());
    }
  });
});

This uses just selected class instead of trying to also manage data-selected attributes.这仅使用selected的类,而不是尝试同时管理data-selected属性。 The code first makes sure only one button in each row is selected.代码首先确保每行中只有一个按钮被选中。 It then populates an Object.然后它填充一个对象。 When two selections have been made, it then creates the parameters for URL.做出两个选择后,它会为 URL 创建参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM