删除确认弹出消息(jQuery)

时间:2016-02-28 21:26:40

标签: jquery css if-statement

我有一个类#include <iostream> #include <iomanip> #include <string> #include <vector> #include <sstream> using namespace std; // set up some variables vector<int> numbers(0); int main() { // Enter numbers (comma delimited). Ex: 1,2,3,4,50,60<return> cout << endl << endl << "Enter numbers (must be comma delimited): "; string nums_in; getline(cin, nums_in); cout << "My numbers are: " << nums_in << endl; string s_tmp = ""; int i_tmp; for(vector<int>::size_type i = 0, len = nums_in.size(); i < len; i++){ if( nums_in[i] == ',' ){ if(s_tmp.size() > 0){ i_tmp = std::stoi(s_tmp); numbers.push_back(i_tmp); } s_tmp = ""; } else if( i == len-1){ s_tmp += nums_in[i]; i_tmp = std::stoi(s_tmp); numbers.push_back(i_tmp); cout << "My parsed numbers are:" << i_tmp << endl; } else { s_tmp += nums_in[i]; } } cout << endl << endl; cout << "Done." << endl; return 0; } 的按钮 当我点击它时,我想运行一个jQuery代码 首先,我想这样做:
.delete

然后我需要使用if / else if / else statments运行一个函数。

  1. 如果点击$('.pop-up-alert').addClass('clicked');,我想这样做:
    .no-btn

  2. 如果$('.pop-up-alert').removeClass('clicked');点击,我想这样做:
    .yes-btn //实际上我只需要删除被点击的$(.delete).parent().remove();类子项的父项(我需要以某种方式使用.delete,但我不知道如何)

    < / LI>
  3. 否则,我再次使用此代码:
    this

  4. 关键是,如果用户点击删除按钮,他会弹出一个能够确认删除的功能。如果他单击YES,jQuery将删除$('.pop-up-alert').removeClass('clicked');父项。如果为NO,则隐藏弹出窗口(CSS中为.delete类)。但如果他点击其他任何地方,无论如何都会隐藏弹出窗口。

    如果我理解正确,代码应该是这样的:

    .clicked

    但它不起作用。 请帮忙

1 个答案:

答案 0 :(得分:0)

为什么不简化这个?不需要if { ... } else { ... }语句。让jQuery检测点击事件,看看我的演示:

&#13;
&#13;
// delete function
$('.delete').click(function() {

  var parent = $(this).parent(); // get parent of delete button
  $('.pop-up-alert').addClass('clicked'); // show the pop-up

  // if user clicks no
  $('.no-btn').click(function() {
    $('.pop-up-alert').removeClass('clicked'); // remove pop-up
  });

  // if user clicks yes
  $('.yes-btn').click(function() {
    parent.remove(); // delete parent
    $('.pop-up-alert').removeClass('clicked'); // hide pop-up
  });
});
&#13;
.pop-up-alert {
  display: none;
  padding: 1em;
  margin: 1em;
  border: 1px solid black;
}
.pop-up-alert.clicked {
  display: block;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="pop-up-alert">
  Are you sure? <a href="#" class="no-btn">No</a> / <a href="#" class="yes-btn">Yes</a>
</div>

<div id="item1">Item 1 <a href="#" class="delete">Delete</a></div>
<div id="item2">Item 2 <a href="#" class="delete">Delete</a></div>
&#13;
&#13;
&#13;

<强> jsFiddle demo

P.S您的代码因为以下行而无效:$(.delete).parent().remove();应该是:$('.delete').parent().remove();