首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据选定的无线电值进行特定查询

根据选定的无线电值进行特定查询
EN

Stack Overflow用户
提问于 2022-02-10 11:31:56
回答 1查看 33关注 0票数 0

我有这样的表格:

代码语言:javascript
复制
<form>
    <input type="text" id="searchedWord" class="form-control form-control-lg" value="words here"/>
    <button type="submit" id="findWord" class="btn btn-primary">Search</button>
    
    <div>
      <input type="radio" value="exact match" id="exact-match">
      <label for="exact-match" class="checked" checked>Exact match</label>
      <input type="radio"  value="all matches" id="all-matches">
      <label for="all-matches">All matches</label>
    </div>
  </form>

因为我不能在这个项目中对单选按钮使用name="",所以我编写了一个脚本来使用类来切换它们。

代码语言:javascript
复制
    let radioBtns = document.querySelectorAll('input[type="radio"]');

    function toggleCheck() {
      radioBtns.forEach(function (radioBtn) {
        radioBtn.classList.toggle('checked');
        radioBtn.classList.contains('checked') ? radioBtn.checked = true : radioBtn.checked = false;
     });
    }

    radioBtns.forEach(function (radioBtn){
    radioBtn.addEventListener('click', toggleCheck, false);
    });

    let anyMatch = document.getElementById('all-matches').classList.contains('checked');
    console.log(anyMatch);
    let exactMatch = document.getElementById('exact-match').classList.contains('checked');
    console.log(exactMatch);

根据所选的收音机,我必须做一个不同的查询。如果选择exact match,输入中的单词将包含引号(queryWithQuotes)。如果选择all matches,输入中的单词不包含引号(queryWithoutQuotes)。

代码语言:javascript
复制
        let pageUrl = window.location.protocol + "//" + window.location.host;
        const searchTerm = document.getElementById("searchedWord").value.replace("'", "''");

        if (exactMatch === true) {
            let queryWithQuotes = pageUrl  + "/_api/search/query?querytext='" + '"' + searchTerm + '"' + "'&selectproperties='Path%2cUrl'&sourceid='xxxxx'";
            console.log(`queryWithQuotes variable is: ${queryWithQuotes }`);
            $.ajax(
                {
                    url: queryWithQuotes,
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: success,
                    error: error
                }
            );
        } else if (anyMatch === true) {
            let queryWithoutQuotes = pageUrl  + "/_api/search/query?querytext='" + searchTerm + "'&selectproperties='Path%2cUrl'&sourceid='xxxxx'";
            console.log(`queryWithoutQuotes variable is: ${queryWithoutQuotes }`);
            $.ajax(
                {
                    url: queryWithoutQuotes,
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: success,
                    error: error
                }
            );
        } else {
            console.log('There is an error');
        }

出于某些原因,搜索只考虑了exact match。即使我切换到All matchesExact match也会继续搜索。

原因何在?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-10 11:56:40

我清理了代码,这在jsfiddle和代码片段中工作得很好。我不知道你是否忘了重要的事情,但也许这会有助于更好地理解你的问题从何而来。我已经评论了我所做的所有改变,可以随意忽略任何仅仅是在这里发布的结果。

代码语言:javascript
复制
  let radioBtns = document.querySelectorAll('input[type="radio"]');

  function toggleCheck() {
    radioBtns.forEach(function (radioBtn) {
      radioBtn.classList.toggle('checked');
      radioBtn.checked = radioBtn.classList.contains('checked') ? true : false; // I fixed this ternary operator.
    });
  }

  radioBtns.forEach(function (radioBtn){
    radioBtn.addEventListener('click', toggleCheck, false);
  });

  function doSearch() {
    // I suspect this is the key issue that you had. The checks for anyMatch and exactMatch need to be inside the function for them to update.
    let anyMatch = document.getElementById('all-matches').classList.contains('checked');
    let exactMatch = document.getElementById('exact-match').classList.contains('checked');
    
    console.log('exact match:', exactMatch, '; any match:', anyMatch);
    
    // ... do the actual code here where you check anyMatch and exactMatch
  }
代码语言:javascript
复制
<input type="text" id="searchedWord" class="form-control form-control-lg" value="words here"/>
<button type="button" onclick="doSearch()" id="findWord" class="btn btn-primary">Search</button> <!-- I changed this button from submit to regular button since you're handling everything in javascript anyway -->
    
<div>
  <input type="radio" value="exact match" id="exact-match" class="checked" checked> <!-- I gave one of the radio buttons a "checked" by default -->
  <label for="exact-match" class="checked" checked>Exact match</label>
  <input type="radio"  value="all matches" id="all-matches">
  <label for="all-matches">All matches</label>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71064346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档