开发者

can ticking checkbox update javascript function call arguments?

开发者 https://www.devze.com 2023-03-09 20:54 出处:网络
I have few pairs of button & check box like on my pages (the combination of the first two arguments in this examole it\'s \'login\' & \'basics\' is unique for each button(

I have few pairs of button & check box like on my pages (the combination of the first two arguments in this examole it's 'login' & 'basics' is unique for each button(

<button class="square_button button_background" type="button" 
           onclick="run_through_ajax('login','basics','false')"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

Can I somehow

  • update onclick="run_through_ajax('login','basics','false')" to onclick="run_through_ajax('login','basics','true')" once the checkbox is ticked?
  • updated onclick="run_through_ajax('login','basics','true')" to onclick="run_through_ajax('login','basics','false')" once the checkbox is un-ticked
  • can I achieve that without introducing ID's for ev开发者_开发知识库ery single button? Can I 'wrap/group' button and checkbox in div so I know what to update?

I can use jQuery, doesn't have to be pure javascript solution.

Every 'run' button runs javascript with different parameters. The checkbox is there to distinguish if one extra action needs to be performed before the run.

can ticking checkbox update javascript function call arguments?


How 'bout this?

HTML:

<input type="hidden" value="login|basic"></input>
<button class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">
<input type="hidden" value="test|advanced"></input>
<button class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">
<input type="hidden" value="login|basic"></input>
<button class="square_button button_background" type="button"> run </button>
<input name="restore" title="restore before ant run" type="checkbox">

Javascript:

function run_through_ajax(p1, p2, p3)
{
    console.log('p1: '+p1+', p2: '+p2+', p3: '+p3);
}

$(document).ready(function(){
    $('button[type=button]').click(function(e){
        var params = $(this).prev('input[type=hidden]').val();
        run_through_ajax(params.split('|')[0], params.split('|')[1], false);
    });
});

$('input[type=checkbox]').click(function(e){
    $(this).prev('button').unbind('click');
    $(this).prev('button').click(function(){
        var params = $(this).prev('input[type=hidden]').val();
        run_through_ajax(params.split('|')[0], params.split('|')[1], e.currentTarget.checked);
    });
});

http://jsfiddle.net/dbrecht/jpySU/

The only dependency here is that the button that you want to toggle the parameter for is the previous button element in the page.

0

精彩评论

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