简体   繁体   English

如何在第三元语句中添加第三个条件?

[英]How can I add a third conditional to ternary statement?

I have a search box with 3 radio buttons. 我有一个带有3个单选按钮的搜索框。 The books and website radio buttons work. 书籍和网站单选按钮起作用。 When you enter a term with either of those radio buttons selected, it performs the search accordingly. 当您选择两个单选按钮之一输入术语时,它将相应地执行搜索。 However, I want to add another condition so that when the second radio button (magazines) is ticked, it performs the search to that url. 但是,我想添加另一个条件,以便在第二个单选按钮(杂志)被打勾时,它将对那个URL执行搜索。 link to my fiddle https://jsfiddle.net/pk4mowfg/ 链接到我的小提琴https://jsfiddle.net/pk4mowfg/

This is the line I believe needs to be fixed in order to achieve the goal 我认为这是必须固定的路线,以实现目标

searchType === 'website'? window.open(websiteUrl, '_blank')  : window.open(catalogUrl, '_blank');
<script>
let searchType = "catalog";
$("#catalog").prop('checked', true);

$("#editsubmit").click(function(e){
  e.preventDefault();
  if ($("#q").val().length > 0){
  let term = $("#q").val().split(' ').join("%20");
  let websiteUrl = 'https://www.delawarelibrary.org/search/node/'+ term;
  let magUrl = 'https://ohioweblibrary.org/search/?q='+ term;
  let catalogUrl = 'https://catalog.clcohio.org/polaris/search/searchresults.aspx?ctx=106.1033.0.0.24&type=Keyword&term='+term;
    searchType === 'website'? window.open(websiteUrl, '_blank') : window.open(catalogUrl, '_blank');
  }
});

$('#frmSearch').keypress(function (e) {
  if ($("#q").val().length > 0){
  if(e.which == 13) {
    e.preventDefault();
    let term = $("#q").val().split(' ').join("%20");
    let websiteUrl = 'https://www.delawarelibrary.org/search/node/'+ term;
    let magUrl = 'https://ohioweblibrary.org/search/?q='+ term;
    let catalogUrl = 'https://catalog.clcohio.org/polaris/search/searchresults.aspx?ctx=106.1033.0.0.24&type=Keyword&term='+term;
    searchType === 'website'? window.open(websiteUrl, '_blank')  : window.open(catalogUrl, '_blank');

    }
  }
});

$("#catalog").click(function(){
  searchType = 'catalog';
});

$("#owl").click(function(){
  searchType = 'mag';
console.log('magclicked');
});


$("#site").click(function(){
  searchType = 'website';
});
</script>

instead of if else statements or nested ternaries you can define a urlMapper object and a function that will map your searchType to urls 可以定义一个urlMapper对象和一个将searchType映射到url的函数,而不是if else语句或嵌套三元数

const urlMapper = {
  website: 'website url',
  catalog: 'catalog url'
}

const getUrl = searchType => urlMapper[searchType] || 'default url';

console.log(
  getUrl('website'),
  getUrl('catalog'),
  getUrl('unknown')
);

You can use another ternary operation for the else portion of the previous ternary to chain them. 您可以对前一个三进制的else部分使用另一个三进制运算来链接它们。

searchType === 'website'? window.open(websiteUrl, '_blank') : 
    searchType === 'catalog' ? window.open(catalogUrl, '_blank') :
        window.open(magUrl, '_blank');

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

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