按钮点击必须在javascript

时间:2017-08-18 03:50:16

标签: javascript jquery html

我有两个相同形式的按钮,一个是提交,另一个是保存到CM,如果用户只是点击保存到CM按钮而没有先点击搜索按钮,我想发出警报。我只是通过传递一个表示点击内容的变量来为这两个onclick事件调用相同的js函数

function setAction(var)
if (var == action)
{
}
else // save to cm 
{
}

我尝试了很多方法但没有任何工作 1.我试图在表单中包含一个隐藏变量,并在if(var == search)中更改了隐藏变量值,但是当我再次单击“保存到cm”时,它会像新调用一样再次进入该函数,并且无法获得之前的函数调用值

  if (action == 'save to cm')
    {
        if(document.getElementById('searchButtonClicked').value==='notclicked')
            {
            alert(document.getElementById('searchButtonClicked').value);
            alert('Please search for the physician before saving to Content 
            Manager');
            return false;
            }
      } 

上述条件总是如此

  1. 我尝试在页面加载时最初禁用该按钮
  2. 在搜索操作功能中,我尝试启用如下按钮 的document.getElementById( 'saveToCm')的removeAttribute( “无效”);

    但是这个根本没有启用按钮。任何帮助表示赞赏

    以下是jsp,我试图删除一些代码,因为它太大了

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE" />
    <META HTTP-EQUIV="EXPIRES" CONTENT="-1" />
    <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE" />
    
    <title></title>
    
    <link rel="stylesheet" href="<c:url value="/css/styles.css"/>" type="text/css"/>
    
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script>
    jQuery(document).ready(function() {
      var specialDates = {};
      var now = new Date();
      specialDates['10' + now.getMonth() + '' + now.getFullYear()] = [true, 'mg-special', 'By Manager'];
      specialDates['25' + now.getMonth() + '' + now.getFullYear()] = [true, 'as-special', 'By Associate'];
      var others = [true, '', ''];
      $("#dateOfSleepStudy").datepicker({
    
        'beforeShowDay': function(date) {
          var target = date.getDate() + '' + date.getMonth() + '' + date.getFullYear();
          return specialDates[target] || others;
        },
        'inline': true
      });
    });
    </script>
    
    
    <script>
    
    
    
        if(typeof String.prototype.trim !== 'function') {
      String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, ''); 
      }
    }
    
    
    
    
    
        function setAction(action) {
    
            var numbers = /^[0-9]+$/;  
             var letters = /[A-Za-z]+$/;  
              var datevalFormat =/^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
    
            if (action == 'SEARCH') {           
                document.getElementById('action').value = action;
                alert(  document.getElementById('saveToCm').disabled);
    
    
        //$("#saveToCm").prop("disabled",false);
          // document.getElementById('saveToCm').removeAttribute("disabled");
    
                //document.getElementById('saveToCm').disabled = false;
    alert(document.getElementById('saveToCm').disabled);
                document.getElementById('searchButtonClicked').value = 'clicked';
                 return checkMandatoryAttribute();      
        } else {
                alert(document.getElementById('saveToCm').disabled);alert('save to cm');
    
            }
            document.getElementById('action').value = action;
            return true;
        }
    
    
    
    
        function checkMandatoryAttribute()
        {
        var numbers = /^[0-9]+$/;  
             var letters = /[A-Za-z]+$/;  
              var datevalFormat =/^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
    
        if (document.getElementById('lastName').value.trim() == '') {
                    alert('Last Name is required.');
                    return false;
                } 
        return true;
        }
    </script>
    
    </head>
    <body>
    <DIV class=headerImg align=left></DIV>
    <br/>
    <table style="width:1200px" class="form">
    
        <tr>
            <td align="center" valign="top">
                <form:form name="SearchForm" commandName="searchForm" action="Search.do" method="post">
                    <table style="width:100%;BORDER-BOTTOM: #6699cc 1px dotted">
                        <tr>
                            <td class="label" align="left"> Last Name</td>
                            <td><form:input  path="lastName" maxlength="50" cssClass="textBox"/></td>
                            <td class="label" align="left">First Name</td>
                            <td><form:input  path="firstName" maxlength="50"  cssClass="textBox"/></td>
                            <td class="label" align="left">State</td>
                            <td><form:input  path="state" maxlength="50" cssClass="textBox"/></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td class="label" align="left">NPI</td>
                            <td><form:input path="npi" maxlength="50" cssClass="textBox"/></td>
                            <td class="label" align="left">City</td>
                            <td><form:input path="city" maxlength="50" cssClass="textBox"/></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td class="label" align="left">Date </td>
                            <td><form:input  id ="dateOfSleepStudy" path="dateOfSleepStudy" cssClass="textBox" required pattern="\d{2}\/\d{2}\/\d{4}"/></td>                        
                        </tr>
    
                        <tr>
                            <td align="right">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</td>
                        </tr>
                        <tr>
                            <td/>
                            <td/>
                            <td/>
                            <td><form:hidden path="action"/></td>
                            <td align="left"><input type="submit" value="search" id="physsearchBtn" name="physsearchBtn" class="buttonDis" onclick="return setAction('SEARCH')"></td>
                            <td><input type="button" value="Reset" class="buttonDis" onClick="doReset()"></td>
    
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                        </tr>
                    </table>
                    <br/>
    
    
    
    
                    <table style="width:450px;">
    
                        <tr>
                            <td colspan="50" align="right">
                                <input type="button" name="saveToCm" id="saveToCm" value="Save To CM" class="buttonDis" disabled=true  onclick="return setAction('SAVETOCM')"/>
                            </td>
                        </tr>
                    </table>
                </form:form>
            </td>
        </tr>
    </table>
    
    </body>
    </html>
    

3 个答案:

答案 0 :(得分:0)

您要做的是在函数之外定义一个设置为false的变量。在单击搜索按钮的条件逻辑内,将此变量设置为true。然后,只需在点击true按钮的条件内检查此值是否为save

这样,在search之前点击save 必须才能触发save逻辑,如下例所示:

var search_clicked = false;

function setAction(action) {
  if (action == 'search') {
    search_clicked = true;
    console.log("Search was clicked");
  }
  if (action == 'save') {
    if (search_clicked == true) {
      // This will only trigger when search has already been clicked
      console.log("Both buttons were clicked");
    }
    else {
      console.log("You need to click search first");
    }
  }
}
<button onclick="setAction('search')">Search</button>
<button onclick="setAction('save')">Save</button>

希望这有帮助! :)

答案 1 :(得分:0)

我认为所有三种措施都是有效的,但您的工具可能会有一些细节上的错误。

例如,在您的第一个想法中,您是否记录了具有全局变体的按钮状态? 正确的处理程序是: 点击了第一个按钮 - &gt;签署全球变化 - &gt;点击了第二个按钮 - &gt;检查全局变化 - &gt;如果全局变体已签名则执行其功能

在你的第二个想法中,你有没有设置你的属性&#34;输入&#34;如[type =&#34;提交&#34;]?如果其类型已提交,则会提交您的表单,因此您无法获得通话值。正确的类型是&#34;按钮&#34;。

您的描述不够准确。如果可能,请粘贴整个代码。

答案 2 :(得分:0)

如果使用jQuery,这很容易。 使用.data()

&#13;
&#13;
$(function() {
  $('#search').on('click', function() {
    console.log('Search button clicked');
    $('#search').data('clicked', true);
  });
  $('#save').on('click', function() {
    console.log('Save button clicked');
    if ($('#search').data('clicked')) {
      //  do something
      console.log('Success..!!!');
    } else {
      // do something
      console.log('Click on Search button to continue');
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='search'>Search</button>
<button id='save'>Save</button>
&#13;
&#13;
&#13;