停止表单提交直到模式在ajax中打开

时间:2017-11-02 04:58:54

标签: javascript jquery ajax event-handling

我从表单和提交按钮获取数据如果数据ios无效则返回false并且如果数据有效我在其成功方法中通过ajax发送电子邮件我显示发送模式或不发送我想要window.location .href在模态被隐藏之后被改变,因为表格提交甚至模态显示之前所以我使用event.preventdefault来阻止它提交但现在有两个问题

  1. 浏览器变得很糟糕,直到模态打开

  2. 在模态window.location没有被更改之后。

  3. 这是我提交按钮的代码

    private TextView greetingTextView;
    private TextView totpoints;
    private TextView totshare;
    private Button btnLogOut;
    private ImageView cardshow;
    private ImageView bmbc;
    private TextView bmbc_text;
    
    SharedPreferences sharedPreferences;
    
    
    // Session Manager Class
    SessionManager session;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);
        setContentView(R.layout.activity_user);
    
    
    
        // Session class instance
        session = new SessionManager(getApplicationContext());
    
    
    
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        Menu menu = bottomNavigationView.getMenu();
        MenuItem menuItem = menu.getItem(1);
        menuItem.setChecked(true);
    
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
    
                    case R.id.navigation_home:
                        Intent intent1 = new Intent(UserActivity.this, Home.class);
                        startActivity(intent1);
                        break;
    
                    case R.id.navigation_card:
    
                        break;
    
                    case R.id.navigation_price:
                        Intent intent3 = new Intent(UserActivity.this, PriceActivity.class);
                        startActivity(intent3);
                        break;
    
                    case R.id.navigation_more:
                        Intent intent4 = new Intent(UserActivity.this, PriceActivity.class);
                        startActivity(intent4);
                        break;
                }
    
    
                return false;
            }
    
    
        });
    
    
    
        greetingTextView = (TextView) findViewById(R.id.greeting_text_view);
        totpoints = (TextView) findViewById(R.id.au_tpresult);
        totshare = (TextView) findViewById(R.id.au_tsresult);
        btnLogOut = (Button) findViewById(R.id.logout_button);
        cardshow = (ImageView) findViewById(R.id.card_stack);
    
    
    
        Intent intent = getIntent();
        String user = intent.getStringExtra("customers_firstname");
        String user1 = intent.getStringExtra("customers_lastname");
        String user2 = intent.getStringExtra("reward_points");
        String user3 = intent.getStringExtra("NoShares");
        String user4 = intent.getStringExtra("CardType_ID");
        String user5 = intent.getStringExtra("Card_No");
    
    
        greetingTextView.setText(user  + " " +  user1);
        totpoints.setText(user2);
        totshare.setText(user3);
    
    
        if (user4 == (null)) {
            ((ImageView) findViewById(R.id.card_stack)).setImageResource(R.color.transparent);
        } else if (user4.equals("0")) {
            ((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_silver);
        } else if (user4.equals("1")) {
            ((ImageView) findViewById(R.id.card_stack)).setImageResource(R.drawable.thar_gold);
    
        }
    
        // Progress dialog
        btnLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                session.logoutUser();
                Toast.makeText(getApplicationContext(), "Session Ended", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(getApplicationContext(), LoginActivityEN.class);
                startActivity(i);
            }
        });
    
        session.checkLogin();
    
        //CARD NUMB
    

    这里是SendEmail()

    <input type="submit"  class="button btn btn-primary" onclick ="return SendEmail();" />
    

    这里是在SendEmail()

    中调用的ajax函数
    function SendEmail()
            {
                if (!CheckContactUsFormValidation()) {
                    return false;
                }
                else{
                    var l_strEmail = document.getElementById('txtEmail').value;
                    var l_strComents = document.getElementById('txtComments').value;
                    var l_strEventLocation = document.getElementById('txtEventLocation').value;
                    var l_strStartDate = document.getElementById('txtStartDate').value;
                    var l_strOrganization = document.getElementById('txtOrganization').value;
                    var l_strPhone = document.getElementById('txtPhone').value;
                    var l_strLastName = document.getElementById('txtLastName').value;
                    var l_strFirstName = document.getElementById('txtFirstName').value;
                var rent = document.getElementById("inlineRadio1").value;
                var lease = document.getElementById("inlineRadio2").value;
                if (rent.checked == true) {
                    var l_strCheck = 'rent';
                }
                else {
                    if (lease.checked == true) {
                        var l_strCheck = 'lease';
                    }
                }
                var l_strTitle = "Contact Us";
    
                var l_strContents = 'Hi, ' + l_strFirstName + ' ' + l_strLastName + '\n';
                l_strContents += 'he is mail for requesting product on ' + l_strCheck + ' and start date is ' + l_strStartDate + ' for organization ' + l_strOrganization + '\n';
                l_strContents += 'his cell #' + l_strPhone + ' and his coments are as follows \n';
                l_strContents += l_strComents + '\n' + 'You can contact him on email: ' + l_strEmail;
                l_strContents += 'End here!';         
                var l_strtoEmail = 'abc@yahoo.com';
                SendEmailWithCustomTitle(l_strtoEmail, l_strContents, l_strTitle);        
            }
            return true;
        }       
    
        function CheckContactUsFormValidation() {
            var m_boolValidator = true; 
    
            if (!CheckRequiredFieldValidation('FirstName', 'Enter first name')) {
                m_boolValidator = false;
            } 
            if (!CheckRequiredFieldValidation('LastName', 'Enter last name')) {
                m_boolValidator = false;
            }
            if (!CheckRequiredFieldValidation('Organization', 'Enter Organization')) {
                m_boolValidator = false;
            }
            if (!CheckRequiredFieldValidation('Email', 'Enter Email')) {
                m_boolValidator = false;
            }
            else
            {
                if (!CheckEmailValidation('Email', 'Enter valid email address')) {
                    m_boolValidator = false;
                }
            }
            if (!CheckRequiredFieldValidation('Phone', 'Enter phone#')) {
                m_boolValidator = false;
            }
            if (!CheckRequiredFieldValidation('StartDate', 'Enter start date')) {
                m_boolValidator = false;
            }
            return m_boolValidator;
        }
    

    我已经尝试过event.preventdefault,event.soppropogation但我找不到任何简单的方法从ajax成功函数SendEmail调用返回true。

2 个答案:

答案 0 :(得分:1)

从您的ajax中删除async:false,以阻止浏览器等待ajax完成

使用以下事件在模式关闭时重定向

$('#myModal').on('hidden.bs.modal', function () {
   window.location.href = "desired-page.php"
})

答案 1 :(得分:0)

您可以在模态和按钮单击事件中创建一个按钮,首先隐藏模态并调用ajax调用。并且window.location.href在html表单中不起作用。要使window.location.href正常工作,您可以将提交按钮移出表单。

要成功打开模态,请遵循以下两种方法之一。

.done(function() {
  $("#yourmodal").modal();
});

或者

success: function(data) {
        $("#yourmodal").modal();
    }

要停止ajax调用,直到模态隐藏使用以下。将ajax调用放在函数中作为示例&#34; submitForm()&#34;。请在下面打电话。

 $('#yourmodal').on('hidden.bs.modal', function (e) {
      // do something...
    })