2017-08-08 22 views
0

我正在通過文本框打開url的腳本,下面是我迄今爲止正在處理的代碼及其失敗的代碼。從文本框中打開url

<form id="main" name="main"> 
    <input type="text" name="inputtext" id="inputtext" placeholder="type here"/> 
    <input type="submit" value="submit" onClick="test();"> 
</form> 
<script> 
function test() { 
    $("main").submit(function(e) { 
     if($('inputtext').val() == 'google'){ 
      // alert('Input can not be left blank'); 
      window.location.href = "https://www.google.com/"; 
     } 
     if($('inputtext').val() == 'yahoo'){ 
      // alert('Input can not be left blank'); 

      window.location.href = "https://www.yahoo.com/"; 
     } 
     if($('inputtext').val() == ''){ 
      alert('Input can not be left blank'); 
     } 
    }); 
} 
</script> 

請諮詢我,如果我錯了工作路徑上還是歡迎使用javascript

+0

也許這個鏈接會幫助你https://stackoverflow.com/questions/32486489/redirect-to -a-url-based-user-input-to-an-autocomplete-field –

+0

我投票將其作爲拼寫錯誤關閉,因爲你錯過了一個'#'來選擇在jQuery –

回答

0
<form id="main" name="main" method="POST"> 
     <input type="text" name="inputtext" id="inputtext" placeholder="type here"/> 
     <input type="button" value="submit" onClick="test();"> 
    </form> 

<script> 
function test() { 
    if(jQuery('#inputtext').val() == 'google'){  

     window.location.href = "https://www.google.com/"; 

    }else if(jQuery('#inputtext').val() == 'yahoo'){ 
    // alert('Input can not be left blank'); 

    window.location.href = "https://www.yahoo.com/"; 

    }else if(jQuery('#inputtext').val() == ''){ 
    alert('Input can not be left blank'); 
    }else if(jQuery('#inputtext').val() != ''){ 
    alert("INVALID"); 
    } 

} 
</script> 
+0

謝謝,它的工作......我 – dcthaju

+0

是的,最簡單的方法是你可以創建更多,如果警報(jQuery '#inputtext')VAL())。 – user3888958

+0

我試圖添加「其他{alert('錯誤的輸入');}它的工作,但如果我輸入」谷歌「這個警告也出現之前指向該網址,任何建議 – dcthaju

0

你的jQuery選擇語法是錯誤的任何建議。要通過其ID選擇一個元素,請在其之前放置一個#

<form id="main" name="main"> 
     <input type="text" name="inputtext" id="inputtext" placeholder="type here"/> 
     <button onClick="test();">Submit</button> 
    </form> 

<script> 
function test() {  

      if($('#inputtext').val() == 'google'){ 
       // alert('Input can not be left blank'); 

      window.location.href = "https://www.google.com/"; 

      } 
     if($('#inputtext').val() == 'yahoo'){ 
       // alert('Input can not be left blank'); 

      window.location.href = "https://www.yahoo.com/"; 

      } 
      if($('#inputtext').val() == ''){ 
       alert('Input can not be left blank'); 



      } 
} 
</script> 
1

您有錯誤的選擇器。難怪你的代碼無法正常工作。 更改$('inputtext').val()分爲以下幾個選擇

所以,如果你想通過其ID的目標元素:

$('#inputtext').val() 

,如果你想通過它的類型來定位元素:

$('input[type="text"]').val(); 

,如果你想目標元素按其類別劃分:

$('.sampleclass').val() 

如果您想要ta它的名字rget元素:

$('input[name="inputtext"]').val() 
1

1)你錯過運營商在你的jQuery電話..,

2)改變你的輸入型submitbutton

3)刪除形式功能,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
 
<form id="main" name="main"> 
 
     <input type="text" name="inputtext" id="inputtext" placeholder="type here"/> 
 
     <input type="button" value="submit" onClick="test();"> 
 
    </form> 
 

 
<script> 
 
function test() { 
 
      if($('#inputtext').val() == 'google'){ 
 
       // alert('Input can not be left blank'); 
 
      console.log('yes') 
 
      window.location.href = "https://www.google.com/"; 
 

 
      } 
 
     if($('#inputtext').val() == 'yahoo'){ 
 
       // alert('Input can not be left blank'); 
 

 
      window.location.href = "https://www.yahoo.com/"; 
 

 
      } 
 
      if($('#inputtext').val() == ''){ 
 
       alert('Input can not be left blank'); 
 
      } 
 
} 
 
</script>

+0

中使用id後添加jquery調用,如果文本框是空的警報出現,但如果鍵入谷歌或雅虎其不工作,請諮詢 – dcthaju

+0

更新其現在的工作.. –

1

一個純JavaScript解決辦法來做到這一點:

var form = document.getElementById('form'); 
 
var input = document.getElementById('url'); 
 

 
form.onsubmit = function(e) { 
 
    e.preventDefault(); 
 
    
 
    switch(input.value) { 
 
    case 'google': 
 
     window.location.href = 'http://google.com' 
 
    break; 
 
    case 'yahoo': 
 
     window.location.href = 'http://yahoo.com' 
 
    break; 
 
    default: 
 
    alert('nope'); 
 
    break; 
 
    } 
 
}
<form id='form'> 
 
    <input type='text' id='url'> 
 
    <input type='submit' value='Go To'> 
 
</form>

+1

我已經投了票關閉,但我沒有足夠的代表徹底關閉它。 @SagarV 我只是想先分享一個純粹的JavaScript解決方案,因爲jQuery是矯枉過正的(對於這項任務) – Albzi