简体   繁体   English

如何解决这个jquery问题?

[英]How can I fix this jquery issue?

I do have a input['text'] field to insert date and also have a dropdown for select category. 我确实有一个input['text']字段来插入日期,也有一个用于选择类别的dropdown

When changing the date input field I want to make the page reload with attaching date and category values in the URL. 更改日期输入字段时,我想重新加载页面,并在URL中附加日期和类别值。

NOTE: Here I want to attach category value if it is not empty. 注意:如果不为空,我想在这里附加类别值。

This is how I tried it. 这就是我尝试过的方式。 But it doesn't work for me. 但这对我不起作用。

$("#date").change(function() {
  var c = $("#category").val();
  var d = $(this).val();
  if(c.length === 0 ) { 
    var cat = "$cat="+c; 
  } else {
    var cat; 
  }

  location.href="?p=attendance&todate="+d.add(cat);
})

This is the output, when category ID is not empty. 当类别ID不为空时,这是输出。

?p=attendance&todate=2016-08-04undefined

Can anybody help me to fix my problem? 有人可以帮我解决我的问题吗? Thank you. 谢谢。

You could try this approach. 您可以尝试这种方法。 Put only what you have. 只放你所拥有的。

$("#date").change(function() { 
    var url = ''; 
    var c = $("#category").val(); 
    var d = $(this).val(); 
    if(c.trim().length) { url+='&cat='+c } 
    if(d.trim().length) { url+='&todate='+d } 
    location.href="?p=attendance"+url; 
})

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

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