开发者

Change value of Onclick function vars

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜