使用 Jquery 进行页面定向

Page orientation with Jquery?

本文关键字:Jquery 使用      更新时间:2023-09-26

单击按钮时;如果页面上的所有文本框都不为空,它将定向到下一页。我使控件有效。但是我如何使用jquery定向到另一个页面呢?

$(document).on("pageinit", "#registerPage1", function () {
    $(".nextBtn").click(function () {
        if ($(".txt").val().lenght != 0) {
            // i want to write codes for orientation registerPage1 to registerPage2 in here 
        }
        $(".txt").each(function () {
            if ($(this).val().length == 0 && $(this).next().attr('class') != 'nullInputMsg') {
                ($(this)).after('<div class="nullInputMsg">this field is required!</div>');
            }
            else if ($(this).val().length != 0 && $(this).next().attr('class') == 'nullInputMsg')
                $(this).next().remove();
        });
    });
});

假设您有一个名为 myform 的表单,其中包含所有文本框。我们还假设带有类nextBtn的按钮位于此窗体内,并触发窗体的提交行为。

像您所做的那样,在提交按钮的click事件上验证表单是可以的。但是,只有在所有验证都通过时,您才希望移动到下一页,因此,您可能应该将重定向部分留到最后,届时您将确定验证检查的结果。在那之后,剩

下要做的就是
  1. 将"myform"的操作属性设置为指向所需的页面。(它重定向到此页面)
  2. 如果验证
  3. 失败,则返回 false,如果验证从处理 click 事件的函数传递,则返回 true。

因此,您的代码看起来像

    $(document).on("pageinit", "#registerPage1", function () {
          $(".nextBtn").click(function () {
              var validationPass = true;
              $(".txt").each(function () {
                  if ($(this).val().length == 0 && $(this).next().attr('class') != 'nullInputMsg') {
                      ($(this)).after('<div class="nullInputMsg">this field is required!</div>');
                      validationPass = false;
                  }
                  else if ($(this).val().length != 0 && $(this).next().attr('class') == 'nullInputMsg')
                      $(this).next().remove();
              });
              return validationPass;
          });
      });

你的 HTML 可能看起来像

     ....
     ....
      <form id="myform" name="myform" action="RedirectToPage.php" method="get">
        ....
        //form content housing the textboxes and button with class .nextBtn
        ....
      </form>
     ....
     ....

我想你所说的方向是指重定向。你不需要jQuery进行重定向。简单的javascript就可以完成这项工作。

// works like the user clicks on a link
window.location.href = "http://google.com";
// works like the user receives an HTTP redirect
window.location.replace("http://google.com");