【问题标题】:Change button function on click using jQuery使用 jQuery 单击更改按钮功能
【发布时间】:2015-06-01 01:07:36
【问题描述】:

不知道我哪里/哪里出错了。一直在尝试在点击时动态更改按钮功能。一种是打开Bootstrap Modal,另一种是作为提交。由于某种原因,if 语句根本没有执行。虽然我对 jQuery 完全陌生并且一直在寻找没有运气。请欣赏任何指针。这是我所拥有的

submitButton.click(function(){
// submitButton.on('click', function() { 
    var e = document.getElementById("ddlAct"), // get value of select option drop down list
        choosenAct = e.options[e.selectedIndex].value;  

    alert("hello"); // to check that this function gets call when  click the button
    alert(choosenAct); // check the value
    if (choosenAct == 'markasgone') {
        alert(choosenAct)
        $('#reasonModal').modal('show');
    } else {
        alert(choosenAct); // add the missing 'n' that cause issue in if stmt
        $("form").submit();
    }
});

上述脚本的结果是:

我在 if 语句和非后记之前收到 2 个警报。我在 if stmt 中放了另一个警报,因为我怀疑 if stmt 没有被执行。 似乎是这种情况,因为我只收到 2 个警报。我错过了什么?

我想要实现的是,如果用户选择“markasgone”选项,那么当点击表单中的 GO 按钮时,它会弹出一个引导模式询问原因,否则只需提交表单。

请帮忙。非常感谢。

更新:感谢 Sergey 发现错字。我现在遇到的问题是没有显示 BootStrap Modal 窗口。我的代码是

<button id="gobutton">Go</button>  

{% comment %}**** Modal ****{% endcomment %}
<div class="modal fade" id="reasonModal" tabindex="-1" role="dialog" aria-labelledby="reasonModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-header"> <h4>Reason Please</h4> </div>
         <div class="modal-body">
            <p> Please let us know your reasons: </p>
            {% include "common/reasons.html" %}
         </div>
         <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>

            <input type="submit" class="btn btn-default" name="confirm" value="Confirm">
         </div>
      </div><!-- /.modal-content -->
   </div>
</div><!-- /.modal -->

【问题讨论】:

  • 我猜问题出在alert(chooseAct);else 下的那个:chooseAct 拼写错误并导致错误。如果您打开它,您可能也可以在调试器控制台中看到它。
  • 谢谢。是的,更改纠正错字现在执行 if stmt。哇..盯着它看了好几个小时,没有看到它。现在 if stmt 确定执行了,我没有设法让模态表单弹出。这是在 jQuery 中调用 Bootstrap Modal 表单的正确方法吗?
  • 现在一切正常。所以这是另一个拼写错误。

标签: jquery html django-templates


【解决方案1】:

注意:第 11 行缺少 n

submitButton.click(function(){
// submitButton.on('click', function() { 
var e = document.getElementById("ddlAct"), // get value of select option drop down list
    choosenAct = e.options[e.selectedIndex].value;  

alert("hello"); // to check that this function gets call when  click the button
alert(choosenAct); // check the value
if (choosenAct == 'markasgone') {
    alert(choosenAct)
    $('#reasonModal').modal('show');
} else {
    alert(choosenAct);
    $("form").submit();
}
});

【讨论】:

  • 谢谢。 if stmt 现在被执行。但是我遇到了一个新问题!未显示引导模式。请提供任何帮助。谢谢
  • 仅供参考 - 引导模型现在可以工作。这是我的代码中的另一个错字。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多