开发者

Change value of Onclick function vars

开发者 https://www.devze.com 2023-02-17 17:27 出处:网络
I have the onClick function attached to a button. I want to be able to change the function var from \'create\' to \'update\' on the page load and through other functions. I have tried the code below b

I have the onClick function attached to a button. I want to be able to change the function var from 'create' to 'update' on the page load and through other functions. I have tried the code below but no luck.

<input type="button" name="save_customer" id="save_customer" value="Save" onClick="customer_crud('create')" />

var开发者_如何转开发 js = "customer_crud('read')";
// create a function from the "js" string
var newclick = new Function(js);
// clears onclick then sets click using jQuery
$("#save_customer").attr('onClick', '').click(newclick)

Any ideas.

UPDATED

Ok in a nutshell i want to change the attr onClick like you would change the attr name & type.

<input type="button" name="save_customer" id="save_customer" value="Save" onClick="customer_crud('create')" />

to

<input type="button" name="save_customer" id="save_customer" value="Save" onClick="customer_crud('update')" />

Thanks


$("#save_customer").click(function(){
    customer_crud("read");
});

Is how you would do this.

EDIT:

$("#save_customer").click(function(){
    customer_crud("update");
});

In jQuery you can set the value of the "onClick" attribute/event with .click(function(){/* Attribute content */});


Don't use inline handlers if possible. They don't get proper scope, and also rely on the highly-frowned upon eval to do their work.

Instead, in your .js file, you can just do this:

var action = 'create';
$('#save_customer').click(function() {
      customer_crud(action);
      action = 'update';
});

The first time the handler is invoked it'll do create, and then subsequently do an update.

I put a working demo at http://jsfiddle.net/QzaXU/

0

精彩评论

暂无评论...
验证码 换一张
取 消