PHP,选中复选框时如何运行链接

php, how to run a link when check box checked?

本文关键字:运行 链接 何运行 复选框 PHP      更新时间:2023-09-26
<input type="checkbox" name="checket" id="checket" />
$url  = 'test.com';

当我检查它时,我希望它转到一个 URL:href='$url'请记住,这已被检查过。

有什么想法吗?

谢谢

编辑:或

if ($('#checket:checked').val() !== undefined) {

在此处插入代码。}

不使用像jquery或mootools这样的JS库,以下是在准系统JS中执行此操作的方法:

<input
    type="checkbox"
    value="<?php echo htmlspecialchars($url) ?>"
    name="checket"
    onClick="if (this.checked) { window.location = this.value; }"
/>

请注意,我已将标签拆分为多行,因此更易于阅读。 基本上,将所需的 URL 嵌入到复选框的值字段中,然后放置一个 onclick 处理程序,该处理程序将读出该 URL 并在复选框变为选中状态时将其提供给 window.location。

加载 jquery 库,然后使用以下代码:

$('input[type=checkbox]#checket').click(function() {
    if($(this).is(':checked')) {
       $.get('urlhere', function(data) {
          // Process return data here
       });
    }
});
在我看来,

你有几个选择。

1)使用javascript和onclick在新窗口中打开链接

http://wpcult.com/open-external-links-in-a-new-window/

2) 设置存储数据的 Cookie 并检查 Cookie 是否存在

http://www.w3schools.com/php/php_cookies.asp

3) 将数据存储在会话变量中

http://www.w3schools.com/php/php_sessions.asp

如果您有多个复选框,请将 URL 放在复选框的值中,并向要执行此操作的所有复选框添加一个类:

<input type="checkbox" name="checket" id="checket" class="checkedurl" value="<?=$url?>" />
<script type="text/javascript">
$(document).ready(function(){
   $('input.checkedurl').click(function() {
      if ($(this).is(':checked')) {
          var checkurl = $(this).val();
          var postvars = "url="+checkurl;
          $.ajax({        
             url: "scripttostore.php",
             type: 'post',
             data: postvars,
             cache: false,
             success: function () {
                location.href = checkurl;
             }
          });
      }
});
</script>

如果页面上的所有复选框您不必使用该类,您只需使用 $('input[type=checkbox]')

scripttostore.php 将是您用来存储 url 信息的存储。

你只能在客户端做

<input type="checkbox" name="checket" id="checket" onclick="location.href='test.com'"/>