在jQuery中,两个按钮的函数是相同的。

时间:2020-11-24 21:23:12

I have two buttons: btnAdd and btnUpdate. I have written a jquery function for button btnUpdate to validate some fields in the web page like:

我有两个按钮:btnAdd和btnUpdate。我已经为按钮btnUpdate编写了jquery函数来验证web页面中的一些字段,比如:

$(function() {

  $('#<%=btnUpdate.ClientID %>').click(function() {
    code here
    });
});

I want to do the same thing when btnAdd is clicked. So I have to write the same function again for btnAdd. Is there other way to avoid this?

我想在单击btnAdd时做同样的事情。所以我要为btnAdd写同样的函数。还有其他方法可以避免这种情况吗?

(If I write one javascript function and call the same function in the button click event of both buttons, it's fine. But is there any way using jQuery?)

(如果我编写一个javascript函数,并在两个按钮的按钮单击事件中调用相同的函数,就可以了。但是使用jQuery有什么方法吗?

4 个解决方案

#1


18  

Just make a selection of two buttons, separated by a comma: ("#add, #update").click... Looks like this in your code:

只需选择两个按钮,用逗号分隔:(“#add, #update”).单击…在你的代码中是这样的:

$(function() { 

  $('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(function() { 
    code here 
    }); 
}); 

#2


7  

There are two ways.

有两种方法。

(1) Select both elements at once and apply the event handler to them:

(1)同时选择两个元素,并对其应用事件处理程序:

$(function() {
    $('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(function() {
        code here
    });
});

(2) Give the function a name, rather than leaving anonymous:

(2)给函数一个名称,而不是留下匿名:

function clickHandler() {
  // code here
}

$('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(clickHandler);

#3


6  

We call this Refactoring and it's something that will help you all the way, read more about, invest in yourself and buy the fantastic one and only Refactoring book

我们称之为重构,它将会帮助你,阅读更多,投资你自己,购买神奇的,唯一的重构书。

in your case, you should do this:

在你的情况下,你应该这样做:

$(function() {

  $('#<%=btnUpdate.ClientID %>').click(function() {
        validate($this);
    });
  $('#<%=btnAdd.ClientID %>').click(function() {
        validate($this);
    });    

});

function validate(myButton)
{
    // code here
}

As you should always do.

就像你应该做的那样。

but as jQuery you can have selectors with multiple inputs, so all you need to do is:

但作为jQuery,您可以使用具有多个输入的选择器,所以您只需:

$('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(function() { 
    validate($this);
}

#4


0  

jQuery is a JavaScript framework, not another language, so you can write one JavaScript function here too.

jQuery是一个JavaScript框架,而不是另一种语言,所以您也可以在这里编写一个JavaScript函数。

function validateFields(e) {
    code here;
}

$(function() {
  $('#<%=btnUpdate.ClientID %>').click(validateFields);
  $('#<%=btnAdd.ClientID %>').click(validateFields);
});

#1


18  

Just make a selection of two buttons, separated by a comma: ("#add, #update").click... Looks like this in your code:

只需选择两个按钮,用逗号分隔:(“#add, #update”).单击…在你的代码中是这样的:

$(function() { 

  $('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(function() { 
    code here 
    }); 
}); 

#2


7  

There are two ways.

有两种方法。

(1) Select both elements at once and apply the event handler to them:

(1)同时选择两个元素,并对其应用事件处理程序:

$(function() {
    $('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(function() {
        code here
    });
});

(2) Give the function a name, rather than leaving anonymous:

(2)给函数一个名称,而不是留下匿名:

function clickHandler() {
  // code here
}

$('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(clickHandler);

#3


6  

We call this Refactoring and it's something that will help you all the way, read more about, invest in yourself and buy the fantastic one and only Refactoring book

我们称之为重构,它将会帮助你,阅读更多,投资你自己,购买神奇的,唯一的重构书。

in your case, you should do this:

在你的情况下,你应该这样做:

$(function() {

  $('#<%=btnUpdate.ClientID %>').click(function() {
        validate($this);
    });
  $('#<%=btnAdd.ClientID %>').click(function() {
        validate($this);
    });    

});

function validate(myButton)
{
    // code here
}

As you should always do.

就像你应该做的那样。

but as jQuery you can have selectors with multiple inputs, so all you need to do is:

但作为jQuery,您可以使用具有多个输入的选择器,所以您只需:

$('#<%=btnUpdate.ClientID %>, #<%=btnAdd.ClientID %>').click(function() { 
    validate($this);
}

#4


0  

jQuery is a JavaScript framework, not another language, so you can write one JavaScript function here too.

jQuery是一个JavaScript框架,而不是另一种语言,所以您也可以在这里编写一个JavaScript函数。

function validateFields(e) {
    code here;
}

$(function() {
  $('#<%=btnUpdate.ClientID %>').click(validateFields);
  $('#<%=btnAdd.ClientID %>').click(validateFields);
});