2017-08-09 42 views
0

如果指向其他網址,則可以使用以下代碼的一些建議代碼正在爲我工​​作,當我輸入文本框並且它將直接指向一個url。如何使用複選框啓用

<script src="jquery.min.js"></script> 
<script> 
function test() { 
    if(jQuery('#inputtext').val() == 'google'){ 
    // alert('Input can not be left blank'); 

    window.location.href = "https://www.google.com/"; 
    } 
    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() != ['google'||'yahoo']){ 
    alert("INVALID Entry"); 
    } 
} 
</script> 

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

是否有可能增加一個複選框,如果複選框與文本輸入檢查的話應該直接到另一個URL。

例如:現在,如果我鍵入谷歌將其引導google.com,如果選中複選框被選中需要的,如果類型爲谷歌就應該直接到gmail.com

形式在下面

<form id="main" name="main"> 
<input type="text" name="inputtext" id="inputtext" placeholder="type here"/> 
<input type="checkbox" name="inputcheckbox" id="inputcheckbox">Redirect 
<input maxlength="10" type="button" value="submit" onClick="test();" ></form> 

請建議..

回答

0

jQuery的1.6+:

您可以使用$('#inputcheckbox').prop('checked'))檢查複選框是否被選中與否。

jQuery的< 1.6:

$('#inputcheckbox').attr('checked'))

我正在使用change事件來檢查條件,所以如果你想在提交時檢查你的條件點擊你可以將其中的代碼複製到你的提交事件/功能。 以下是我的示例代碼。

$(function() { 
 

 
    $('#inputtext,#inputcheckbox').change(function() { 
 

 
    if ($('#inputtext').val() == 'google' && 
 
     $('#inputcheckbox').prop('checked')) { 
 
     alert('redirecting to google...') 
 
     window.location.href = "https://www.google.com/"; 
 
    } 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id="main" name="main"> 
 
    <input type="text" name="inputtext" id="inputtext" placeholder="type here" /> 
 
    <input type="checkbox" name="inputcheckbox" id="inputcheckbox"> 
 

 
    <input maxlength="10" type="button" value="submit" onClick="AddPrinter();"> 
 
</form>

+0

謝謝你,它的工作對我來說 – Thaju

0

添加複選框具有不同ID的

<input type="checkbox" id="isAgeSelected"/> 

然後使用SC RIPT

if(document.getElementById('isAgeSelected').checked) { 
    window.location.href = // Some URL 
} 

另一種方式是

$('#isAgeSelected').click(function() { 
    window.location.href = // Some URL 
}); 
0
<script> 
function test() { 
    if(jQuery('#inputtext').val() == 'google' && jQuery('#inputcheckbox').isChecked){ 
    // alert('Input can not be left blank'); 

    window.location.href = "https://www.google.com/"; 
    } 
    if(jQuery('#inputtext').val() == 'yahoo' && jQuery('#inputcheckbox').isChecked){ 
    // 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() != ['google'||'yahoo']){ 
    alert("INVALID Entry"); 
    } 
} 
</script> 

我希望我的回答可以幫助你。

1

我不確定我是否理解您的問題,但似乎您想在用戶輸入「google」並選中複選框時將重定向URL更改爲www.gmail.com。

可以實現這一目標的方式如下:

if($('#inputtext').val() == 'google' && $('#inputcheckbox').isChecked) { 
    window.location.href = "https://www.gmail.com/"; 
} 

PS:你可以在你的代碼中使用$而不是jQuery,它具有相同的效果,並保持代碼更加清晰。