动态替换href

时间:2016-01-26 12:14:01

标签: javascript jquery html css

我的网站中有以下代码行 - CodePen

我想要做的是,在下载项目之前,用户必须首先填写表单:

  <div class="container">
    <!-- Trigger the modal with a button -->
    <a href="#example1" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#myModal">Download Item #1</a>

    <a href="#example2" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Download Item #2</a>

    <!-- Modal -->
    <div class="modal fade" id="myModal" role="dialog">
      <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <form role="form">
              <div class="form-group">
                <label for="email">Email address:</label>
                <input type="email" class="form-control" id="email">
              </div>
              <div class="form-group">
                <label for="pwd">Password:</label>
                <input type="password" class="form-control" id="pwd">
              </div>
              <div class="checkbox">
                <label>
                  <input type="checkbox"> Remember me</label>
              </div>
              <button type="submit" class="btn btn-default">Submit</button>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>

如何使用当用户选择页面上的第一个或第二个按钮时,它会将此信息发送到提交按钮?

2 个答案:

答案 0 :(得分:0)

您可以在JavaScript中为按钮添加点击处理程序:

var fieldsAreReadyToBeSent = function() {
    // Validation code goes here
    // return true if valid & false if invalid
};

document.querySelector("[type=submit]").addEventListener("click", function() {
    if(!fieldsAreReadyToBeSent()) {
        event.preventDefault();
    }
});

方法event.preventDefault()会阻止您的表单在被视为无效时被提交!

演示:

var fieldsAreReadyToBeSent = function() {
    return false; // FOR THIS DEMO, ALWAYS RETURN FALSE
};

document.querySelector("[type=submit]").addEventListener("click", function() {
    if(!fieldsAreReadyToBeSent()) { // FOR THIS DEMO, THIS IS ALWAYS TRUE
        event.preventDefault();
    }
});
 <div class="container">
<!-- Trigger the modal with a button -->
<a href="#example1" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#myModal">Download Item #1</a>

<a href="#example2" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Download Item #2</a>

<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <form role="form">
          <div class="form-group">
            <label for="email">Email address:</label>
            <input type="email" class="form-control" id="email">
          </div>
          <div class="form-group">
            <label for="pwd">Password:</label>
            <input type="password" class="form-control" id="pwd">
          </div>
          <div class="checkbox">
            <label>
              <input type="checkbox"> Remember me</label>
          </div>
          <button type="submit" class="btn btn-default">Submit</button>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
  </div>

答案 1 :(得分:0)

您可以将链接存储在变量中。然后,单击该按钮时,检查字段是否为空(并应用任何验证规则)。如果不为空,请将提交应用于表单。

$link = 'example.com/download.pdf';

$('.button').click(function(){
    // CHECK THE FIELDS ARE NOT EMPTY
    // APPLY YOUR OWN VALIDATION

    if($field1 = $('.field1').val() != '' && $('.field2').val() != ''){
       // DO WHAT YOU NEED TO DO WITH THE LINK
       $('form').submit();
    } 
})

在旁注中,您可能因为没有向我们提供解决您自己问题的尝试而被投票(而非我自己)。您向我们提供了您的HTML,但没有提供任何经过测试的逻辑。