0

http://jsfiddle.net/B9Fub/

<tr class="fis">
Test: <input type="text" name="test" id="test" value="Test">
<tr class="fis">
Empty: <input type="text" name="empty" id="empty">
<input type="button" value="find" id="find">

$('#find').click(function() {
    alert('wont work if any other fields are filled other than TEST'); 
});

$('#test').change(function() {
    // needs to fill the form for other functionality
    $('#empty').val('empty'); 
    alert('change');
});

所以在这里我有在第一个输入“更改”时填充的字段

但是,如果他们在第一个字段中输入并单击“查找”按钮,我不希望它运行 [更改]。

我想知道是否有办法做到这一点。根据我的发现,如果单击“查找”按钮,则“更改”会发生在“查找”按钮之前。

我尝试将“更改”更改为“焦点”并将其提供给另一个字段,因此第一个字段没有事件处理程序,但这可能会给我带来更多麻烦。

我需要填写这些字段,但如果用户单击查找按钮,我不希望它们填写。

4

3 回答 3

0

不过,不确定你想要什么。也许这会有所帮助。

var frm = $('form')[0]; //assuming you have one form on that page
var fnd = $('#find');
fnd.click(function(){
  var te = 0, fe = 0;
  if(frm.elements[0].value !== '')te = 1;
  for(var i=1,l=frm.length; i<l; i++){
    if(frm.elements[i].value !== '')fe = 1;
  }
  if(fe === 1){
    return false;
  }
  else{
    // do your thing here
    return true; // some editors show an error if you don't always return a value
  }
});
$('#test').change(function(){
  if(fnd.val() !== ''){
    return false;
  }
  else{
    // do your thing here
    return true;
  }
});
于 2013-09-09T23:56:34.283 回答
0

The mousedown event fires before the change event, so by using that and jQuery's data() method you can set a flag to see if the activeElement is in fact #test when the find button is clicked, and if so don't set the value :

$('#find').on('mousedown', function() {
    $('#test').data('flag', document.activeElement.id === 'test');
});

$('#test').on('change', function() {
    if ( ! $(this).data('flag') ) $('#empty').val('empty'); 
});

FIDDLE

于 2013-09-09T23:13:59.503 回答
0

我找到了答案。使用 e.stopPropagation(); on $('#find') 允许我阻止 .change() 发生

于 2013-10-15T17:55:40.970 回答