2011-05-20 96 views
0

我只知道我需要什么,但我不知道如何完成。Javascript逻辑问题

这是代码的逻辑,我真的希望你们中的一些人有解决方案。

如何在JavaScript或jQuery中创建将执行以下操作的函数?

If that checkbox is selected, when the button is clicked redirect the user to another page by passing the value of the textarea in the URL. 

这就是逻辑。

我们有三个要素。

1)复选框

2)输入类型按钮

3)textarea的。

选中该复选框,用户单击该按钮,用户转到另一页面,URL将包含在textarea中找到的值。

http://mydomainname/page.php?ValueThatWasinTextArea=Hello World 

你能帮助我。

我认为这是一个简单的JavaScript编码器。

谢谢你这么多

回答

1
$(function(){ 
    $(':button').click(function(){ 
    if($('input[type="checkbox"]').is(":checked")){ 
     window.location.href = "http://mydomainname/page.php?ValueThatWasinTextArea="+ $('textarea').val(); 
    } 
    }); 
}); 

**当然,如果还有比页面上的这三个要素越多,你会想一些更具体的选择

+0

+ 1提供一个解决方案,无论按钮是否提交表单都可以工作。 – Neil 2011-05-20 13:29:42

1

如果复选框被选中,您可以订阅submit活动形式和内部测试,如果是使用window.location.href重定向到所需的URL:

$('#id_of_the_form').submit(function() { 
    var value = encodeURIComponent($('#id_of_textarea').val()); 
    if ($('#id_of_checkbox').is(':checked')) { 
     window.location.href = '/page.php?ValueThatWasinTextArea=' + value; 
     return false; 
    } 
}); 

如果该按钮不是提交按钮,您可以订阅该按钮的click事件并执行相同的逻辑。

0

可能是一些语法问题,因为我在我的头顶上这个代码

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

<button id="myButton" onClick="buttonClick" /> 

<input id="myTextArea" type="textarea" /> 

<script> 

    function buttonClick() 
    { 
    var checkBox = document.getElementById('myCheckbox'); 
    var textArea = document.getElementById('myTextArea'); 


    if(checkBox.checked) 
    { 
     window.location = 'http://mydomainname/page.php?ValueThatWasinTextArea=' + textArea.value; 
    } 
    } 

</script> 
0
$(document).ready(function() { 
    $('#btnSubmit').click(function() { 
     if($('#chkBox').is(':checked')) { 
      window.location = '/page.php?passedValue=' + $('#txtField').val(); 
     } 
    }); 
}; 

...

<form> 
    <p> 
     <input type="checkbox" id="chkBox"> Checkbox</input> 
    </p> 
    <p> 
     <input type="text" id="txtField" value="" /> 
    </p> 
    <p> 
     <input type="submit" id="btnSubmit" value="Submit" /> 
    </p> 
</form>