JS将复选框值添加到URL然后单击转到URL

时间:2016-04-08 13:54:17

标签: javascript jquery

我有一个看起来像这样的脚本:

var baseUrl = 'http://ab3.0e7.myftpupload.com/idmatch_process.php?idmatch_process.php?quizid=". urlencode($quiz_id) ."&escalation=". urlencode($escalation) ."&correctrule=". urlencode($correctrule) ."&page=" . $next_page ."&ans=';

$(document).ready(function () {

// listen to change event (customize selector to your needs)
$('input[type=checkbox]').change(function (e) {

    e.preventDefault();

    if ($(this).is(':checked')) {

        // read in value
        var queryString = $(this).val();

        // loop through siblings (customize selector to your needs)
        var s = $(this).siblings();
        $.each(s, function () {

            // see if checked
            if ($(this).is(':checked')) {

                // append value
                queryString += ' OR ' + $(this).val();
            }
        });

        // jump to url
        window.location = baseUrl + queryString;
    }
});

});

基本上我得到了复选框的值,然后将它附加到baseUrl和。然后我会自动更改窗口位置。我想要做的是让窗口位置更改不是复选框单击,而是单击我要添加的按钮。有人有一个简单的方法吗?

3 个答案:

答案 0 :(得分:1)

如果您将queryString的声明移到外部范围,您可以稍后点击使用它。

$(document).ready(function () {
  var queryString = '';

  // listen to change event (customize selector to your needs)
  $('input[type=checkbox]').change(function (e) {

    e.preventDefault();

    if ($(this).is(':checked')) {
      // read in value
      queryString = $(this).val();

      // loop through siblings (customize selector to your needs)
      var s = $(this).siblings();
      $.each(s, function () {
        // see if checked
        if ($(this).is(':checked')) {
          // append value
          queryString += ' OR ' + $(this).val();
        }
      });
    }
  });

  $('button.submit').on('click', function() {
    // jump to url
    window.location = baseUrl + queryString;
  });
});

答案 1 :(得分:1)

将您的ulr存储在全局变种

仅当网址不为空并且点击时重定向时才显示该按钮。

var baseUrl = 'http://ab3.0e7.myftpupload.com/idmatch_process.php?idmatch_process.php?quizid=". urlencode($quiz_id) ."&escalation=". urlencode($escalation) ."&correctrule=". urlencode($correctrule) ."&page=" . $next_page ."&ans=';

var newurl="";

$(document).ready(function () {

// listen to change event (customize selector to your needs)
$('input[type=checkbox]').change(function (e) {

    e.preventDefault();

    if ($(this).is(':checked')) {

        // read in value
        var queryString = $(this).val();

        // loop through siblings (customize selector to your needs)
        var s = $(this).siblings();
        $.each(s, function () {

            // see if checked
            if ($(this).is(':checked')) {

                // append value
                queryString += ' OR ' + $(this).val();
            }
        });

        // jump to url
       newurl = baseUrl + queryString; //setting new url
       $("#yourbutton").show();
    }
});

  $("#yourbutton").click(function(){
     if(newurl!="")
       window.location.href=newurl;
}

});

答案 2 :(得分:0)

您可以创建一个按钮,onclick可以在文档就绪时执行您正在执行的操作,该按钮应该可以正常工作。