admin管理员组

文章数量:1410737

I have a link with id="something":

html

<a id="something" onclick="getReturnValue()"> Something </a>

javascript

function getReturnValue(){
  //some validation
  if(validation fails){
    return false;
  }else{
    return true;
  }
}

I need to get the return value when I use:

$("#something").click();

How can I get the return value for the event ?

Already onclick function is mentioned in the html tag. I wanted to use:

$("#something").click(); 

and from that I wanted to get return value.

As I have to use it in multiple place, I dont want to write getReturnValue() method again and again like:

click(function(){ getReturnValue() })

I need to do this

if($("#something").click() == true){
  form.submit();
}

click() will call getReturnValue() which will do some validation. If the validation fails, it return false and I need not submit the form. If validation is true, I need to submit the form

I have a link with id="something":

html

<a id="something" onclick="getReturnValue()"> Something </a>

javascript

function getReturnValue(){
  //some validation
  if(validation fails){
    return false;
  }else{
    return true;
  }
}

I need to get the return value when I use:

$("#something").click();

How can I get the return value for the event ?

Already onclick function is mentioned in the html tag. I wanted to use:

$("#something").click(); 

and from that I wanted to get return value.

As I have to use it in multiple place, I dont want to write getReturnValue() method again and again like:

click(function(){ getReturnValue() })

I need to do this

if($("#something").click() == true){
  form.submit();
}

click() will call getReturnValue() which will do some validation. If the validation fails, it return false and I need not submit the form. If validation is true, I need to submit the form

Share Improve this question edited Jun 8, 2012 at 14:30 Tivakar asked Jun 7, 2012 at 22:40 TivakarTivakar 1636 silver badges15 bronze badges 4
  • I can't seem to figure out why you want to retrieve the value from the event handler. – nebulousGirl Commented Jun 7, 2012 at 23:30
  • I need to do this if($("#something").click() == true){ form.submit(); } click() will call getReturnValue() which will do some validation. If the validation fails, it return false and I need not submit the form. If validation is true, I need to submit the form – Tivakar Commented Jun 8, 2012 at 14:16
  • If you want to do some form validation before form submission, you just took the wrong approach. You are plicating your code. See my answer below. – nebulousGirl Commented Jun 8, 2012 at 15:45
  • @nebulousGirl There are lots of other languages/frameworks that are able to return values from an event. .NET and Wordpress (do_action) are a couple notable examples. It is more applicable with custom events (.trigger('some_event')) than with DOM events. I like to trigger custom events before+after AJAX calls so different ponents can hook into the event, possibly modify the data, and possibly alter the resulting action. For example, my default response to an ajax call might be to reload the page but in certain situations, I may want to redirect instead. – Jeff Commented Aug 16, 2017 at 22:15
Add a ment  | 

5 Answers 5

Reset to default 1

To do some form validation, you can do something like that:

$('#myForm').submit(function(e) {
    if () {//Validation rules failed
        e.preventDefault(); //Prevent browsers from submitting
    }
    //Form is valid do nothing and let the form submit
});

Now on your button:

$("#something").click(function() {$('#myForm').submit();//Calls function from above});

What you can do is create a jQuery event object and pass that to the click handler.

var e = jQuery.Event('click');
$('#something').click(e)

if (e.validResult) {
    //action
}

You'll have to modify the click handler somewhat to take an event object and set a validResult property if the validation is successful.

function getReturnValue(e) {
    //some validation
    if (valid) {
        e.validResult= true;
    }
}

Here is an example on how you can access return data from event handler.

var a;
$(document).on('a', function(e){
    $(document).trigger(a = $.Event('sum', {datas: e.datas}));
});
$(document).on('sum', function(e){
    return e.datas;
});
$(document).trigger($.Event('a', {datas: [1,2,3]}));
console.log(a.result);//[1,2,3]

You change it with your purpose. check here https://api.jquery./event.result/ for more details.

$("#something").click(function() {
    var x = getReturnValue();
});

this is what you want?

LE: and this means your a changes to

<a id="something"> Something </a>

you're trying to run two different functions on a click event, maybe you need remove the inline code (onclick="func()") and put this in your script

$("#something").click(
  function(){
    var returnValue = getReturnValue();
  }
);

I don't really understand your question though...

本文标签: javascriptHow to get jQuery event handler return valueStack Overflow