How to pass variable to href in javascript?

前端 未结 5 1651
没有蜡笔的小新
没有蜡笔的小新 2021-01-04 09:24

How to pass this variable value here? Below code is not working. And all other discussions on Stackoverflow are unclear.



        
相关标签:
5条回答
  • 2021-01-04 09:27

    You have fetch value of the field, Currently you are using DOM object

    Use

     var dist = document.getElementById('value').value;
    

    OR

    Use

     if (dist.value!=""){
         window.location.href="district.php?dist="+dist.value;
    

    instead of

    if (dist!=""){
         window.location.href="district.php?dist="+dist;
    
    0 讨论(0)
  • 2021-01-04 09:29

    You have to fetch field value using .value as you are passing whole object to the URL as document.getElementbyId('value') returns whole field object.

    var dist = document.getElementById('value').value;
    

    So your function should be like this

    function check() {
        var dist = document.getElementById('value').value; // change here
        if (dist != "") {
            window.location.href = "district.php?dist=" + dist;
        } else
            alert('Oops.!!');
    }
    
    0 讨论(0)
  • 2021-01-04 09:29

    Try this:

    var dist = document.getElementById('value').value;
    if (dist != "") {
     window.location.href="district.php?dist="+dist;
    }
    
    0 讨论(0)
  • 2021-01-04 09:48

    Try this:

    function check() {
        var dist = document.getElementById('value').value;
    
        if (dist) {
            window.location.href = "district.php?dist=" + dist;
        } else {
            alert('Oops.!!');
        }
    }
    
    0 讨论(0)
  • 2021-01-04 09:54

    You have to make some correction in your function..

    function check()
        {
            var dist = document.getElementById('value').value;  //for input text value
           if (dist!==""){  //  for comparision
               window.location.href="district.php?dist="+dist;
           }
           else
               alert('Oops.!!');
        }
    
    0 讨论(0)
提交回复
热议问题